Part Number Hot Search : 
E15CA 30N60 E15CA BSY92 VEC2812 KRA120S BYW99 RLR7843
Product Description
Full Text Search
 

To Download ATTINY2313A-MU Datasheet File

  If you can't view the Datasheet, Please click here to try to view without PDF Reader .  
 
 


  Datasheet File OCR Text:
  features ? high performance, low power avr ? 8-bit microcontroller ? advanced risc architecture ? 120 powerful instructions ? mo st single clock cycle execution ? 32 x 8 general purpose working registers ? fully static operation ? up to 20 mips throughput at 20 mhz ? data and non-volatile program and data memories ? 2/4k bytes of in-system self programmable flash ? endurance 10,000 write/erase cycles ? 128/256 bytes in-sys tem programmable eeprom ? endurance: 100,000 write/erase cycles ? 128/256 bytes internal sram ? programming lock fo r flash program and eeprom data security ? peripheral features ? one 8-bit timer/counter with separate prescaler and compare mode ? one 16-bit timer/counter with separate prescaler, compare and capture modes ?four pwm channels ? on-chip analog comparator ? programmable watchdog timer with on-chip oscillator ? usi ? universal serial interface ? full duplex usart ? special microcontroller features ? debugwire on-chip debugging ? in-system programmable via spi port ? external and internal interrupt sources ? low-power idle, power-down, and standby modes ? enhanced power-on reset circuit ? programmable brown-out detection circuit ? internal calibrated oscillator ? i/o and packages ? 18 programmable i/o lines ? 20-pin pdip, 20-pin soic, 20-pad mlf/vqfn ? operating voltage ? 1.8 ? 5.5v ? speed grades ? 0 ? 4 mhz @ 1.8 ? 5.5v ? 0 ? 10 mhz @ 2.7 ? 5.5v ? 0 ? 20 mhz @ 4.5 ? 5.5v ? industrial temperature range: -40 c to +85 c ? low power consumption ? active mode ? 190 a at 1.8v and 1mhz ?idle mode ? 24 a at 1.8v and 1mhz ? power-down mode ? 0.1 a at 1.8v and +25 c 8-bit microcontroller with 2/4k bytes in-system programmable flash attiny2313a attiny4313 preliminary rev. 8246a?avr?11/09
2 8246a?avr?11/09 attiny2313a/4313 1. pin configurations figure 1-1. pinout attiny2313a/4313 1.1 pin descriptions 1.1.1 vcc digital supply voltage. 1.1.2 gnd ground. 1.1.3 port a (pa2..pa0) port a is a 3-bit bi-directional i/o port with inte rnal pull-up resistors (selected for each bit). the port a output buffers have symmetrical drive characteristics with both high sink and source capability, except pa2 which has the reset capability. to use pin pa2 as i/o pin, instead of reset pin, program (?0?) rstdisbl fuse. as input s, port a pins that ar e externally pulled low (pcint10/reset/dw) pa2 (pcint11/rxd) pd0 (pcint12/txd) pd1 (pcint9/xtal2) pa1 (pcint8/clki/xtal1) pa0 (pcint13/ckout/xck/int0) pd2 (pcint14/int1) pd3 (pcint15/t0) pd4 (pcint16/oc0b/t1) pd5 gnd 20 19 18 17 16 15 14 13 12 11 1 2 3 4 5 6 7 8 9 10 vcc pb7 (usck/scl/sck/pcint7) pb6 (miso/do/pcint6) pb5 (mosi/di/sda/pcint5) pb4 (oc1b/pcint4) pb3 (oc1a/pcint3) pb2 (oc0a/pcint2) pb1 (ain1/pcint1) pb0 (ain0/pcint0) pd6 (icpi/pcint17) pdip/soic 1 2 3 4 5 mlf/vqfn 15 14 13 12 11 20 19 18 17 16 6 7 8 9 10 (pcint12/txd) pd1 (pcint9/xtal2) pa1 (pcint8/clki/xtal1) pa0 (pcint13/ckout/xck/int0) pd2 (pcint14/int1) pd3 (pcint15/t0) pd4 (pcint16/oc0b/t1) pd5 gnd (pcint17/icpi) pd6 (ain0/pcint0) pb0 pb5 (mosi/di/sda/pcint5) pb4 (oc1b/pcint4) pb3 (oc1a/pcint3) pb2 (oc0a/pcint2) pb1 (ain1/pcint1) pd0 (rxd/pcint11) pa2 (reset/dw/pcint10) vcc pb7 (usck/scl/sck/pcint7) pb6 (miso/do/pcint6) note: bottom pad should be soldered to ground.
3 8246a?avr?11/09 attiny2313a/4313 will source current if the pull-up resistors are activated. the port a pins are tri-stated when a reset condition becomes active, even if the clock is not running. port a also serves the functions of various spec ial features of the atti ny2313a/4313 as listed on page 61 . 1.1.4 port b (pb7..pb0) port b is an 8-bit bi-directional i/o port with inte rnal pull-up resistors (selected for each bit). the port b output buffers have symmetrical drive characteristics with both high sink and source capability. as inputs, port b pins that are externally pulled low will source current if the pull-up resistors are activated. the port b pins are tri-stated when a reset condition becomes active, even if the clock is not running. port b also serves the functions of various spec ial features of the atti ny2313a/4313 as listed on page 62 . 1.1.5 port d (pd6..pd0) port d is a 7-bit bi-directional i/o port with inte rnal pull-up resistors (selected for each bit). the port d output buffers have symmetrical drive c haracteristics with bot h high sink and source capability. as inputs, port d pins that are exte rnally pulled low will source current if the pull-up resistors are activated. the port d pins are tri-stated when a reset condition becomes active, even if the clock is not running. port d also serves the function s of various special features of the attiny2313a/4313 as listed on page 66 . 1.1.6 reset reset input. a low level on this pin for longer than the minimum pulse length will generate a reset, even if the clock is not running and prov ided that the reset pin has not been disabled. the minimum pulse length is given in table 21-3 on page 198 . shorter pulses are not guaranteed to generate a reset. the reset input is an alternate function for pa2 and dw. the reset pin can also be used as a (weak) i/o pin. 1.1.7 xtal1 input to the inverting oscillator amplifier and input to the internal clock operating circuit. xtal1 is an alternate function for pa0. 1.1.8 xtal2 output from the inverting osc illator amplifier. xtal2 is an alternate func tion for pa1.
4 8246a?avr?11/09 attiny2313a/4313 2. overview the attiny2313a/4313 is a low-power cmos 8- bit microcontroller based on the avr enhanced risc architecture. by executing powerful in structions in a single clock cycle, the attiny2313a/4313 achieves throughputs approach ing 1 mips per mhz allowing the system designer to optimize power consum ption versus processing speed. 2.1 block diagram figure 2-1. block diagram program counter program flash instruction register gnd vcc instruction decoder control lines stack pointer sram general purpose register alu status register programming logic spi 8-bit data bus xtal1 xtal2 reset internal oscillator oscillator watchdog timer timing and control mcu control register mcu status register timer/ counters interrupt unit eeprom usi usart analog comparator data register portb data dir. reg. portb data register porta data dir. reg. porta portb drivers pb0 - pb7 porta drivers pa0 - pa2 data register portd data dir. reg. portd portd drivers pd0 - pd6 on-chip debugger internal calibrated oscillator
5 8246a?avr?11/09 attiny2313a/4313 the avr core combines a rich instruction set wit h 32 general purpose working registers. all the 32 registers are directly connected to the arit hmetic logic unit (alu), allowing two independent registers to be accessed in one single instruction executed in one clock cycle. the resulting architecture is more code efficient while achiev ing throughputs up to ten times faster than con- ventional cisc microcontrollers. the attiny2313a/4313 provides the following features: 2/4k bytes of in-system programmable flash, 128/256 bytes eeprom, 12 8/256 bytes sram, 18 general pu rpose i/o lines, 32 general purpose working registers, a single-wire interface for on-chip debugging, two flexible timer/counters with compare mo des, internal and external interrupts, a serial programmable usart, universal serial interface with start condition detector, a programmable watchdog timer with internal oscillator, an d three software selectable powe r saving modes. the idle mode stops the cpu while allowing the sram, timer/count ers, and interrupt system to continue func- tioning. the power-down mode saves the register contents but freezes th e oscillator, disabling all other chip functions until the next interrupt or hardware reset. in standby mode, the crys- tal/resonator oscillator is running while the rest of the device is sleeping. this allows very fast start-up combined with low-power consumption. the device is manufactured using atmel?s high density non-volatile memory technology. the on-chip isp flash allows the program memory to be reprogrammed in-system through an spi serial interface, or by a conventional non-vol atile memory programmer. by combining an 8-bit risc cpu with in-system self-programm able flash on a monolithic chip, the atmel attiny2313a/4313 is a powerful microcontroller that provides a highly flexible and cost effective solution to many embedded control applications. the attiny2313a/4313 avr is supported with a full suite of program and system development tools including: c compilers, ma cro assemblers, program debugger/simulators, in-circuit emu- lators, and evaluation kits. 2.2 comparison between at tiny2313a and attiny4313 the attiny2313a and attiny4313 differ only in memory sizes. table 2-1 summarizes the differ- ent memory sizes for the two devices. table 2-1. memory size summary device flash eeprom ram attiny2313a 2k bytes 128 bytes 128 bytes attiny4313 4k bytes 256 bytes 256 bytes
6 8246a?avr?11/09 attiny2313a/4313 3. about 3.1 resources a comprehensive set of drivers, application notes, data sheets and descriptions on development tools are available for downloa d at http://www.a tmel.com/avr. 3.2 code examples this documentation contains simple code examples that briefly show how to use various parts of the device. these code examples assume that the part specific header file is included before compilation. be aware that not all c compiler vendors include bit definitions in the header files and interrupt handling in c is compiler dependent. please confirm with the c compiler documen- tation for more details. for i/o registers located in the extended i/o map, ?in?, ?out?, ?sbis?, ?sbic?, ?cbi?, and ?sbi? instructions must be replaced with instructions that allow access to extended i/o. typically, this means ?lds? and ?sts? combined with ?sbrs?, ?s brc?, ?sbr?, and ?cbr?. note that not all avr devices include an extended i/o map. 3.3 data retention reliability qualification results sh ow that the projected data retention failure rate is much less than 1 ppm over 20 years at 85c or 100 years at 25c. 3.4 disclaimer typical values contained in th is datasheet are based on simula tions and characterization of other avr microcontrollers manufactured on th e same process technology. min and max values will be available after the devi ce has been characterized.
7 8246a?avr?11/09 attiny2313a/4313 4. cpu core this section discusses the avr core architecture in general. the main function of the cpu core is to ensure correct program execution. the cpu must therefore be able to access memories, perform calculations, control peri pherals, and handle interrupts. 4.1 architectural overview figure 4-1. block diagram of the avr architecture in order to maximize performance and parallelism, the avr uses a harvard architecture ? with separate memories and buses for program and data. instructions in the program memory are executed with a single level pipe lining. while one instruction is being executed, the next instruc- tion is pre-fetched from the program memory. this concept enables instructions to be executed in every clock cycle. the program memory is in-system reprogrammable flash memory. the fast-access register file contains 32 x 8-bit general purpose working registers with a single clock cycle access time. this allows single-cycle ar ithmetic logic unit (alu ) operation. in a typ- ical alu operation, two operands are output from the register file, the operation is executed, and the result is stored back in the register file ? in one clock cycle. flash program memory instruction register instruction decoder program counter control lines 32 x 8 general purpose registrers alu status and control i/o lines eeprom data bus 8-bit data sram direct addressing indirect addressing interrupt unit spi unit watchdog timer analog comparator i/o module 2 i/o module1 i/o module n
8 8246a?avr?11/09 attiny2313a/4313 six of the 32 registers can be used as three 16 -bit indirect address register pointers for data space addressing ? enabling efficient address calculations. one of the these address pointers can also be used as an address pointer for look up tables in flash program memory. these added function registers are the 16-bit x-, y-, and z-register, described later in this section. the alu supports arithmetic and logic operations between registers or between a constant and a register. single register operations can also be executed in the alu. after an arithmetic opera- tion, the status register is up dated to reflect information about the result of the operation. program flow is provided by conditional and uncon ditional jump and call instructions, able to directly address the whole addres s space. most avr instructions have a single 16-bit word for- mat. every program memory address co ntains a 16- or 32-bit instruction. during interrupts and subroutine calls, the return address program counter (pc) is stored on the stack. the stack is effectively allocated in th e general data sram, and consequently the stack size is only limited by the total sram size an d the usage of the sram. all user programs must initialize the sp in the reset routine (before subroutines or interrupts are executed). the stack pointer (sp) is read/write accessible in the i/o space. the data sram can easily be accessed through the five different addressing mo des supported in the avr architecture. the memory spaces in the avr architecture are all linear and regular memory maps. a flexible interrupt module has its control r egisters in the i/o spac e with an additional global interrupt enable bit in the status register. all interrupts have a s eparate interrupt vector in the interrupt vector table. the interrupts have priority in accordance with their interrupt vector posi- tion. the lower the interr upt vector address, the higher the priority. the i/o memory space contains 64 addresses for cpu peripheral functi ons as control regis- ters, and other i/o functions. the i/o memory can be accessed directly, or as the data space locations following those of the register file, 0x20 - 0x5f. 4.2 alu ? arithm etic logic unit the high-performance avr alu operates in dire ct connection with all the 32 general purpose working registers. within a single clock cycle, arithmetic operations between general purpose registers or between a register and an immedi ate are executed. the alu operations are divided into three main categories ? arithmetic, logical, and bit-functions. some implementations of the architecture also provide a powerful multip lier supporting both signed/unsigned multiplication and fractional format. see the ?instruction set? section for a detailed description. 4.3 status register the status register contains information about th e result of the most recently executed arithme- tic instruction. this information can be used for altering program flow in order to perform conditional operations. note that the status re gister is updated after all alu operations, as specified in the instructio n set reference. this will in many cases remove the need for using the dedicated compare instructions, resulting in faster and more compact code. the status register is not automatically st ored when entering an interrupt routine and restored when returning from an interrupt. this must be handled by software.
9 8246a?avr?11/09 attiny2313a/4313 the avr status register ? sreg ? is defined as: ? bit 7 ? i: global interrupt enable the global interrupt enable bit must be set for th e interrupts to be enabled. the individual inter- rupt enable control is then performed in separate control registers. if the global interrupt enable register is cleared, none of t he interrupts are enabled independent of the individual interrupt enable settings. the i-bit is cleared by hardware after an interrupt has occurred, and is set by the reti instruction to enable subsequent interr upts. the i-bit can also be set and cleared by the application with the sei and cli instructions, as described in the instructio n set reference. ? bit 6 ? t: bit copy storage the bit copy instructions bld (bit load) and bst (b it store) use the t-bi t as source or desti- nation for the operated bit. a bit from a register in the register file can be copied into t by the bst instruction, and a bit in t can be copied into a bit in a register in the register file by the bld instruction. ? bit 5 ? h: half carry flag the half carry flag h indicates a half carry in so me arithmetic operations. half carry is useful in bcd arithmetic. see the ?instruction set description? for detailed information. ? bit 4 ? s: sign bit, s = n v the s-bit is always an exclusive or between the negative flag n and the two?s complement overflow flag v. see the ?instruction set description? for detailed information. ? bit 3 ? v: two?s complement overflow flag the two?s complement overflow flag v suppor ts two?s complement arithmetics. see the ?instruction set description? for detailed information. ? bit 2 ? n: negative flag the negative flag n indicates a negative result in an arithmetic or logic operation. see the ?instruction set description? for detailed information. ? bit 1 ? z: zero flag the zero flag z indicates a zero result in an arithmetic or logic operation. see the ?instruction set description? for detailed information. ? bit 0 ? c: carry flag the carry flag c indicates a carry in an arithmetic or logic operation. see the ?instruction set description? for de tailed information. 4.4 general purpose register file the register file is optimized for the avr enhanc ed risc instruction set. in order to achieve the required performance and flex ibility, the following in put/output schemes ar e supported by the register file: ? one 8-bit output operand and one 8-bit result input bit 76543210 0x3f (0x5f) i t h s v n z c sreg read/write r/w r/w r/ wr/wr/wr/wr/wr/w initial value 0 0 0 0 0 0 0 0
10 8246a?avr?11/09 attiny2313a/4313 ? two 8-bit output operands and one 8-bit result input ? two 8-bit output operands and one 16-bit result input ? one 16-bit output operand and one 16-bit result input figure 4-2 shows the structure of the 32 genera l purpose working registers in the cpu. figure 4-2. avr cpu general purpose working registers most of the instructions operating on the register file have direct access to all registers, and most of them are single cycle instructions. as shown in figure 4-2 , each register is also assigned a data memory address, mapping them directly into the first 32 loca tions of the user data space. although not being physically imple- mented as sram locations, this memory organizati on provides great flexibility in access of the registers, as the x-, y- and z-pointer registers can be set to index any register in the file. 4.4.1 the x-register, y-register, and z-register the registers r26..r31 have some added functi ons to their general purpose usage. these reg- isters are 16-bit address pointers for indirect addressing of the data space. the three indirect address registers x, y, and z are defined as described in figure 4-3 . figure 4-3. the x-, y-, and z-registers 7 0 addr. r0 0x00 r1 0x01 r2 0x02 ? r13 0x0d general r14 0x0e purpose r15 0x0f working r16 0x10 registers r17 0x11 ? r26 0x1a x-register low byte r27 0x1b x-register high byte r28 0x1c y-register low byte r29 0x1d y-register high byte r30 0x1e z-register low byte r31 0x1f z-register high byte 15 xh xl 0 x-register 7 0 7 0 r27 (0x1b) r26 (0x1a) 15 yh yl 0 y-register 7 0 7 0 r29 (0x1d) r28 (0x1c) 15 zh zl 0 z-register 7 0 7 0 r31 (0x1f) r30 (0x1e)
11 8246a?avr?11/09 attiny2313a/4313 in the different addressing modes these address registers have functions as fixed displacement, automatic increment, and automatic decrement (s ee the instruction set reference for details). 4.5 stack pointer the stack is mainly used for storing temporary data, for storing local variables and for storing return addresses after interrupts and subroutine calls. the stack pointer register always points to the top of the stack. note that the stack is implemented as growing from higher memory loca- tions to lower memory location s. this implies that a stack push command decreases the stack pointer. the stack pointer points to the data sram stack area where the subroutine and interrupt stacks are located. this stack space in the da ta sram must be defined by the program before any subroutine calls are executed or interrupt s are enabled. the stack pointer must be set to point above 0x60. the stack pointer is decrement ed by one when data is pushed onto the stack with the push instruction, and it is decremented by two when the return address is pushed onto the stack with subroutine call or interrupt. the st ack pointer is incremented by one when data is popped from the stack with the pop instruction, and it is incremented by two when data is popped from the stack with return from subr outine ret or return from interrupt reti. the avr stack pointer is implemented as two 8- bit registers in the i/o space. the number of bits actually used is implementation dependent. no te that the data space in some implementa- tions of the avr architecture is so small that only spl is needed. in this case, the sph register will not be present. 4.6 instruction execution timing this section describes the general access timing concepts for instructi on execution. the avr cpu is driven by the cpu clock clk cpu , directly generated from the selected clock source for the chip. no internal clo ck division is used. figure 4-4 shows the parallel instruction fetches and instruction executions enabled by the har- vard architecture and the fast-acc ess register file concept. this is the basic pipelining concept to obtain up to 1 mips per mhz with the corr esponding unique results for functions per cost, functions per clocks, and functions per power-unit. bit 151413121110 9 8 ????????sph 0x3d (0x5d) sp7 sp6 sp5 sp4 sp3 sp2 sp1 sp0 spl 76543210 read/writerrrrrrrr r/w r/w r/w r/w r/w r/w r/w r/w initial value ramend ramend ramend ramend ramend ramend ramend ramend ramend ramend ramend ramend ramend ramend ramend ramend
12 8246a?avr?11/09 attiny2313a/4313 figure 4-4. the parallel instruction fetche s and instruction executions figure 4-5 shows the internal timing concept for the register file. in a single clock cycle an alu operation using two register operands is executed, and the result is stored back to the destina- tion register. figure 4-5. single cycle alu operation 4.7 reset and in terrupt handling the avr provides several different interrupt sources. these interrupts and the separate reset vector each have a separate program vector in the program memory space. all interrupts are assigned individual enable bits which must be writ ten logic one together with the global interrupt enable bit in the status register in order to enable the interrupt. the lowest addresses in the program memory space are by default defined as the reset and interrupt vectors. the complete list of vectors is shown in ?interrupts? on page 47 . the list also determines the priority levels of the different interrupts. the lower the address the higher is the priority level. reset has the highest priority, and next is int0 ? the external interrupt request 0. refer to ?interrupts? on page 47 for more information. when an interrupt occurs, the global interrupt enable i-bit is cleared and all interrupts are dis- abled. the user software can write logic one to the i-bit to enable nested interrupts. all enabled interrupts can then interrupt the current interrupt routine. the i-bit is automatically set when a return from interrupt instru ction ? reti ? is executed. there are basically two types of interrupts. the fi rst type is triggered by an event that sets the interrupt flag. for these interrup ts, the program counter is vector ed to the actual interrupt vector in order to execute the interrupt handling rout ine, and hardware clears the corresponding inter- rupt flag. interrupt flags can also be cleared by wr iting a logic one to the flag bit position(s) to be cleared. if an interrupt condition occurs while the corresponding interrupt enable bit is cleared, clk 1st instruction fetch 1st instruction execute 2nd instruction fetch 2nd instruction execute 3rd instruction fetch 3rd instruction execute 4th instruction fetch t1 t2 t3 t4 cpu total execution time register operands fetch alu operation execute result write back t1 t2 t3 t4 clk cpu
13 8246a?avr?11/09 attiny2313a/4313 the interrupt flag will be set and re membered until the inte rrupt is enabled, or the flag is cleared by software. similarly, if one or more interrupt condit ions occur while the global interrupt enable bit is cleared, the corresponding interrupt flag(s) w ill be set and remembered until the global interrupt enable bit is set, and will th en be executed by order of priority. the second type of interrupts will trigger as long as the interrupt condition is present. these interrupts do not necessarily have interrupt flags. if the interrupt condition disappears before the interrupt is enabled, the in terrupt will not be triggered. when the avr exits from an inte rrupt, it will always retu rn to the main pr ogram and execute one more instruction before any pending interrupt is served. note that the status register is not automatically stored when entering an interrupt routine, nor restored when returning from an interrupt routine. this must be handled by software. when using the cli instruction to disable interrupts, the interrup ts will be immediately disabled. no interrupt will be ex ecuted after the cli instru ction, even if it occurs simultaneously with the cli instruction. the following example shows how th is can be used to avoid interrupts during the timed eeprom write sequence.. when using the sei instruction to enable interr upts, the instruction following sei will be exe- cuted before any pending interrupts, as shown in this example. assembly code example in r16, sreg ; store sreg value cli ; disable interrupts during timed sequence sbi eecr, eempe ; start eeprom write sbi eecr, eepe out sreg, r16 ; restore sreg value (i-bit) c code example char csreg; csreg = sreg; /* store sreg value */ /* disable interrupts during timed sequence */ __disable_interrupt(); eecr |= (1< 14 8246a?avr?11/09 attiny2313a/4313 4.7.1 interrupt response time the interrupt execution response for all the enabled avr interrupts is four clock cycles mini- mum. after four clock cycles the program vector address for the actual interrupt handling routine is executed. during this four clock cycle perio d, the program counter is pushed onto the stack. the vector is normally a jump to the interrupt routine, and this jump takes three clock cycles. if an interrupt occurs during execution of a multi- cycle instruction, this in struction is completed before the interrupt is served. if an interrupt occurs when the mcu is in sleep mode, the interrupt execution response time is increased by four clo ck cycles. this increase co mes in addition to the start-up time from the selected sleep mode. a return from an interrupt handling routine take s four clock cycles. during these four clock cycles, the program counter (two bytes) is po pped back from the stack, the stack pointer is incremented by two, and the i-bit in sreg is set.
15 8246a?avr?11/09 attiny2313a/4313 5. memories this section describes the different memories in the attiny2313a/4313. the avr architecture has two main memory spaces, the data memory and the program memory space. in addition, the attiny2313a/4313 features an eeprom memo ry for data storage. all three memory spaces are linear and regular. 5.1 in-system reprogrammabl e flash program memory the attiny2313a/4313 contains 2/4k bytes on-chip in-system reprogrammable flash memory for program storage. since all avr instructions ar e 16 or 32 bits wide, the flash is organized as 1/2k x 16. the flash memory has an endurance of at least 10,000 write/erase cycles. the attiny2313a/4313 program counter (pc) is 10/11 bits wide, thus addressing the 1/2k program memory locations. ?memory programming? on page 178 contains a detailed description on flash data serial downloading using the spi pins. constant tables can be allocated within the entire program memory address space (see the lpm ? load program memory instruction description). timing diagrams for instruction fetc h and execution are presented in ?instruction execution tim- ing? on page 11 . figure 5-1. program memory map 0x0000 0x03ff/0x07ff program memory
16 8246a?avr?11/09 attiny2313a/4313 5.2 sram data memory figure 5-2 shows how the attiny2313a/4313 sram memory is organized. the lower 224/352 data memory locations address both th e register file, the i/o memory, extended i/o memory, and the internal data sr am. the first 32 locati ons address the register file, the next 64 location the standard i/o me mory, and the next 128/256 locations address the internal data sram. the five different addressing modes for the data memory cover: direct, indirect with displace- ment, indirect, indirect with pre-decrement, and indirect with post-increment. in the register file, registers r26 to r31 feature the indirect addressing pointer registers. the direct addressing reaches the entire data space. the indirect with displacement mode reaches 63 address locations from the base address given by the y- or z-register. when using register indirect addressing modes with automatic pre-decrement and post-incre- ment, the address registers x, y, and z are decremented or incremented. the 32 general purpose working registers, 64 i/o registers, and the 128/ 256 bytes of internal data sram in the attiny2313a/4313 are all acce ssible through all these addressing modes. the register file is described in ?general purpose register file? on page 9 . figure 5-2. data memory map 5.2.1 data memory access times this section describes the general access timi ng concepts for internal memory access. the internal data sram access is performed in two clk cpu cycles as described in figure 5-3 . 32 registers 64 i/o registers internal sram (128/256 x 8) 0x0000 - 0x001f 0x0020 - 0x005f 0x00df/15f 0x0060 data memory
17 8246a?avr?11/09 attiny2313a/4313 figure 5-3. on-chip data sram access cycles 5.3 eeprom data memory the attiny2313a/4313 contains 128/256 bytes of data eeprom memory. it is organized as a separate data space, in which single bytes can be read and written. the eeprom has an endurance of at least 100,000 write/erase cycles. the access between the eeprom and the cpu is described in the following, specif ying the eeprom address registers, the eeprom data register, and the eeprom control register. for a detailed description of serial data downloading to the eeprom, see page 190 . 5.3.1 eeprom read/write access the eeprom access registers are accessible in the i/o space. the write access times for the eeprom are given in table 5-1 on page 22 . a self-timing func- tion, however, lets the user software detect w hen the next byte can be written. if the user code contains instructions that writ e the eeprom, some precautions mu st be taken. in heavily fil- tered power supplies, v cc is likely to rise or fall slo wly on power-up/down. this causes the device for some period of time to run at a volt age lower than specified as minimum for the clock frequency used. see ?preventing eeprom corruption? on page 20 for details on how to avoid problems in these situations. in order to prevent unintentional eeprom writes, a specific write procedure must be followed. refer to ?atomic byte programming? on page 17 and ?split byte programming? on page 18 for details on this. when the eeprom is read, the cpu is halted for fo ur clock cycles before the next in struction is executed. when the eeprom is written, the cpu is halte d for two clock cycles before the next instruction is executed. 5.3.2 atomic byte programming using atomic byte programming is the simplest mode. when writing a by te to the eeprom, the user must write the address in to the eearl register and data into eedr register. if the clk wr rd data data address address valid t1 t2 t3 compute address read write cpu memory access instruction next instruction
18 8246a?avr?11/09 attiny2313a/4313 eepmn bits are zero, writing eepe (within four cycles after eempe is written) will trigger the erase/write operation. both the erase and writ e cycle are done in one operation and the total programming time is given in table 5-1 on page 22 . the eepe bit remains set until the erase and write operations are completed. while the devi ce is busy with progra mming, it is not possi- ble to do any other eeprom operations. 5.3.3 split byte programming it is possible to split the erase and write cycle in two different operations. this may be useful if the system requires short access time for some limited period of ti me (typically if the power sup- ply voltage falls). in order to take advantage of th is method, it is required that the locations to be written have been erased before the write oper ation. but since the erase and write operations are split, it is possible to do the erase operations when the system allows doing time-critical operations (typically after power-up). 5.3.4 erase to erase a byte, t he address must be written to eearl. if the eepmn bits are 0b01, writing the eepe (within four cycles after eempe is written) will trigger the er ase operation on ly (program- ming time is given in table 5-1 on page 22 ). the eepe bit remains se t until the erase operation completes. while the device is busy programming, it is not possible to do any other eeprom operations. 5.3.5 write to write a location, the user must write the address into eearl and the data into eedr. if the eepmn bits are 0b10, writing the eepe (within four cycles after eempe is written) will trigger the write operation only (programming time is given in table 5-1 on page 22 ). the eepe bit remains set until the write operat ion completes. if the location to be written has not been erased before write, the data that is stored must be considered as lost. while the device is busy with programming, it is not possible to do any ot her eeprom operations. the calibrated oscillator is used to time the eeprom accesses. make sure the oscillator fre- quency is within the requirements described in ?osccal ? oscillator ca libration register? on page 30 .
19 8246a?avr?11/09 attiny2313a/4313 5.3.6 program examples the following code examples show one assembly and one c function for erase, write, or atomic write of the eeprom. the examples assume that interrupts are controlled (e.g., by disabling interrupts globally) so that no interrupts will occur during ex ecution of th ese functions. note: 1. see ?code examples? on page 6. assembly code example (1) eeprom_write: ; wait for completion of previous write sbic eecr,eepe rjmp eeprom_write ; set programming mode ldi r16, (0<>eepm0) /* set up address and data registers */ eearl = ucaddress; eedr = ucdata; /* write logical one to eempe */ eecr |= (1< 20 8246a?avr?11/09 attiny2313a/4313 the next code examples show assembly and c functions for reading the eeprom. the exam- ples assume that interrupts are controlled so that no interrupts will occur during execution of these functions. note: 1. see ?code examples? on page 6. 5.3.7 preventing eepr om corruption during periods of low v cc , the eeprom data can be corrupted because the supp ly voltage is too low for the cpu and the eeprom to operate properly. these issues are the same as for board level systems using eepr om, and the same design so lutions should be applied. an eeprom data corruption can be caused by two situations when the volt age is too low. first, a regular write sequence to the eeprom requires a minimum voltage to operate correctly. sec- ondly, the cpu itself can execut e instructions incorr ectly, if the supply voltage is too low. eeprom data corruption can easily be avoided by follo wing this design recommendation: keep the avr reset active (low) during periods of insufficient power supply voltage. this can be done by enabling the internal brown-out detect or (bod). if the detection level of the internal bod does not match the needed detection level, an external low v cc reset protection circuit can be used. if a reset occurs while a write operation is in progress , the write operation will be com- pleted provided that the power supply voltage is sufficient. assembly code example (1) eeprom_read: ; wait for completion of previous write sbic eecr,eepe rjmp eeprom_read ; set up address (r17) in address register out eearl, r17 ; start eeprom read by writing eere sbi eecr,eere ; read data from data register in r16,eedr ret c code example (1) unsigned char eeprom_read( unsigned char ucaddress) { /* wait for completion of previous write */ while(eecr & (1< 21 8246a?avr?11/09 attiny2313a/4313 5.4 i/o memory the i/o space definition of the attiny2313a/4313 is shown in ?register summary? on page 252 . all attiny2313a/4313 i/os and peripherals are pl aced in the i/o space. all i/o locations may be accessed by the ld/lds/ldd and st/sts/std instructions, transferring data between the 32 general purpose working registers and the i/o space. i/o registers wi thin the address range 0x00 - 0x1f are directly bit-acce ssible using the sbi and cbi instru ctions. in these registers, the value of single bits can be checked by using the sbis and sbic instructions. refer to the instruction set section for more details. when us ing the i/o specific commands in and out, the i/o addresses 0x00 - 0x3f must be used. when addr essing i/o registers as data space using ld and st instructions, 0x20 must be added to these addresses. for compatibility with future devices, reserved bits should be written to zero if accessed. reserved i/o memory addresses should never be written. some of the status flags are clear ed by writing a logical one to th em. note that, unlike most other avrs, the cbi and sbi instructions will only oper ate on the specified bit, and can therefore be used on registers containing such status flags. the cbi and sbi instructions work with registers 0x00 to 0x1f only. the i/o and peripherals control regist ers are explained in later sections. 5.4.1 general purpose i/o registers the attiny2313a/4313 contains three general pu rpose i/o registers. these registers can be used for storing any information, and they are pa rticularly useful for st oring global variables and status flags. general purpose i/o registers within the address range 0x00 - 0x1f are directly bit-accessible using the sbi, cbi, sbis, and sbic instructions. 5.5 register description 5.5.1 eear ? eeprom address register ? bit 7 ? eear7: eeprom address this is the most significant eeprom address bit of attiny4313. in devices with less eeprom, i.e. attiny2313a, this bit is reserved and will always read zero. the initial value of the eeprom address register (eear) is undefined and a proper value must therefore be written before the eeprom is accessed. ? bits 6..0 ? eear6..0: eeprom address these are the (low) bits of the eeprom a ddress register. the eeprom data bytes are addressed linearly in the range 0...(128/256-1). the initial value of eear is undefined and a proper value must be therefore be written before the eeprom may be accessed. bit 76543210 0x1e (0x3e) eear7 eear6 eear5 eear4 eear3 eear2 eear1 eear0 eear read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value x x x x x x x x
22 8246a?avr?11/09 attiny2313a/4313 5.5.2 eedr ? eeprom data register ? bits 7..0 ? eedr7..0: eeprom data for the eeprom write operation the eedr regist er contains the data to be written to the eeprom in the address given by the eear regi ster. for the eeprom read operation, the eedr contains the data read out from the eeprom at the add ress given by eear. 5.5.3 eecr ? eeprom control register ? bit 7 ? res: reserved bit this bit is reserved for future use and will always read as 0 in attiny2313a/4313. for compatibil- ity with future avr devices, always write this bit to zero. afte r reading, mask out this bit. ? bit 6 ? res: reserved bit this bit is reserved in the attiny23 13a/4313 and will always read as zero. ? bits 5, 4 ? eepm1 and eepm0: eeprom programming mode bits the eeprom programming mode bits setting def ines which programming action that will be triggered when writing eepe. it is possible to program data in one atomic operation (erase the old value and program the new value) or to split the erase and write operations in two different operations. the programming times for the different modes are shown in table 5-1 . when eepe is set any write to eepmn will be ignored. during reset, the eepmn bits will be reset to 0b00 unless the eeprom is busy programming. ? bit 3 ? eerie: eeprom ready interrupt enable writing eerie to one enables the eeprom ready interrupt if the i-bit in sreg is set. writing eerie to zero disables the interrupt. the eep rom ready interrupt gener ates a constant inter- rupt when non-volatile memory is ready for programming. ? bit 2 ? eempe: eeprom master program enable the eempe bit determines whether writing eepe to o ne will have effect or not. bit 76543210 0x1d (0x3d) eedr7 eedr6 eedr5 eedr4 eedr3 eedr2 eedr1 eedr0 eedr read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 bit 76543210 0x1c (0x3c) ? ? eepm1 eepm0 eerie eempe eepe eere eecr read/write r r r/w r/w r/w r/w r/w r/w initial value 0 0 x x 0 0 x 0 table 5-1. eeprom programming mode bi ts and programming times eepm1 eepm0 programming time operation 0 0 3.4 ms erase and write in one operation (atomic operation) 0 1 1.8 ms erase only 1 0 1.8 ms write only 1 1 ? reserved for future use
23 8246a?avr?11/09 attiny2313a/4313 when eempe is set, setting eepe within four cl ock cycles will program the eeprom at the selected address. if eempe is zero, setting eepe will have no effect. when eempe has been written to one by software, hardware clears the bit to zero after four clock cycles. ? bit 1 ? eepe: eeprom program enable the eeprom program enable signal eepe is th e programming enable signal to the eeprom. when eepe is written, the ee prom will be programmed accord ing to the eepmn bits setting. the eempe bit must be written to one before a logical one is written to eepe, otherwise no eeprom write takes place. when the write a ccess time has elapsed, the eepe bit is cleared by hardware. when eepe has been set, the cpu is halted for two cycles before the next instruction is executed. ? bit 0 ? eere: eeprom read enable the eeprom read enable signal ? eere ? is t he read strobe to the eeprom. when the cor- rect address is set up in the eear register, the eere bit must be written to one to trigger the eeprom read. the eeprom read access takes one instruction, and the requ ested data is available immediately. when t he eeprom is read, the cpu is ha lted for four cycles before the next instruction is executed. th e user should poll the eepe bit be fore starting the read opera- tion. if a write operation is in progress, it is neither possible to read the eeprom, nor to change the eear register. 5.5.4 gpior2 ? general purpose i/o register 2 5.5.5 gpior1 ? general purpose i/o register 1 5.5.6 gpior0 ? general purpose i/o register 0 bit 76543210 0x15 (0x35) msb lsb gpior2 read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 bit 76543210 0x14 (0x34) msb lsb gpior1 read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 bit 76543210 0x13 (0x33) msb lsb gpior0 read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000
24 8246a?avr?11/09 attiny2313a/4313 6. clock system figure 6-1 presents the principal clock systems in the avr and their distribution. all of the clocks need not be active at a given time. in order to reduce power consumption, the clocks to modules not being used can be halted by using different sleep modes, as described in ?power manage- ment and sleep modes? on page 33 . figure 6-1. clock distribution 6.1 clock subsystems 6.1.1 cpu clock ? clk cpu the cpu clock is routed to parts of the syst em concerned with operat ion of the avr core. examples of such modules are the general pur pose register file, the status register and the data memory holding the stack po inter. halting the cpu clock inhi bits the core from performing general operations and calculations. 6.1.2 i/o clock ? clk i/o the i/o clock is used by the majority of the i/o modules, like timer/counters, and usart. the i/o clock is also used by the external interrupt module, but note that some external interrupts are detected by asynchronous logic, allowing such interrupts to be detected even if the i/o clock is halted. also note that start condition detectio n in the usi module is carried out asynchronously when clk i/o is halted, enabling usi start condition detection in all sleep modes. 6.1.3 flash clock ? clk flash the flash clock controls operation of the flash interface. the flash clock is usually active simul- taneously with the cpu clock. general i/o modules cpu core ram clk i/o avr clock control unit clk cpu flash and eeprom clk flash source clock watchdog timer watchdog oscillator reset logic clock multiplexer watchdog clock calibrated rc oscillator crystal oscillator external clock
25 8246a?avr?11/09 attiny2313a/4313 6.2 clock sources the device has the following clock source options, selectable by flash fuse bits as shown below. the clock from the selected source is inpu t to the avr clock generator, and routed to the appropriate modules. note: 1. for all fuses ?1? means unprogrammed while ?0? means programmed. the various choices for each clocking option is given in the following sections. when the cpu wakes up from power-down, the se lected clock source is used to time the start-up, ensuring sta- ble oscillator operation before instruction execution starts. when the cpu starts from reset, there is an additional delay allowing the power to reach a stable level before commencing nor- mal operation. the watchdog oscillator is used for timing this real-time part of the start-up time. 6.2.1 default clock source the device is shipped with cksel = ?0100?, sut = ?10?, and ckdiv8 programmed. the default clock source setting is the internal rc oscillator with longes t start-up time and an initial system clock prescaling of 8, resulting in 1.0 mhz syst em clock. this default setting ensures that all users can make their desired clock source setting using an in-system or parallel programmer. for low-voltage devices it should be noted that unprogramming the ckdiv8 fuse may result in overclocking. at low voltages (b elow 2.7v) the devices are rated for maximum 4 mhz operation (see section 21.3 on page 196 ), but routing the clock signal from the internal oscillator directly to the system clock line will run the device at 8 mhz. 6.2.2 external clock to drive the device from an external clock source, xtal1 should be driven as shown in figure 6-2 . to run the device on an ex ternal clock, the cksel fuses must be programme d to ?0000?. table 6-1. device clocking select device clocking option cksel3..0 (1) external clock (see page 25 ) 0000 calibrated internal rc oscillator 4 mhz (see page 26 ) 0010 calibrated internal rc oscillator 8 mhz (see page 26 ) 0100 128 khz internal oscillator (see page 27 )0110 external crystal/ceramic resonator (see page 28 ) 1000 - 1111 reserved 0001/0011/0101/0111
26 8246a?avr?11/09 attiny2313a/4313 figure 6-2. external clock drive configuration when this clock source is sele cted, start-up times are determi ned by the sut fuses as shown in table 6-2 . when applying an external clock, it is required to avoid sudden changes in the applied clock fre- quency to ensure stable operation of the mcu. a variation in frequency of more than 2% from one clock cycle to the next can lead to unpredictable behavior. it is required to en sure that the mcu is kept in reset during such changes in the clock frequency. note that the system clock prescaler can be used to implement run-time changes of the internal clock frequency while still ensuring stable operation. see ?system clock prescaler? on page 29 for details. 6.2.3 calibrated inte rnal rc oscillator the calibrated internal rc oscillator provides a fixed 8.0 mhz clock. the frequency is nominal value at 3v and 25 c. if 8 mhz frequency exceeds the spec ification of the device (depends on v cc ), the ckdiv8 fuse must be programmed in orde r to divide the internal frequency by 8 dur- ing start-up. the device is shipped with t he ckdiv8 fuse programmed. this clock may be selected as the system clock by progr amming the cksel fuses as shown in table 6-3 . if selected, it will operate with no external com ponents. during reset, hardware loads the calibra- tion byte into the osccal register and thereby automatically calibrates the rc oscillator. at 3v and 25 c, this calibration gives a frequency with in 10% of the nominal frequency. using calibration methods as described in application no tes available at www.atmel.com/avr it is possi- ble to achieve 2% accuracy at any given v cc and temperature. when this oscillator is used as the chip clock, the watchdog oscillator will still be used for the watchdog timer and for the table 6-2. start-up times for the external clock selection sut1..0 start-up time from power- down and power-save additional delay from reset recommended usage 00 6 ck 14ck bod enabled 01 6 ck 14ck + 4 ms fast rising power 10 6 ck 14ck + 64 ms slowly rising power 11 reserved nc external clock signal xtal2 xtal1 gnd
27 8246a?avr?11/09 attiny2313a/4313 reset time-out. for more information on the pre-programmed calibration value, see the section ?calibration byte? on page 181 . note: 1. the device is shipped with this option selected. when this oscillator is select ed, start-up times are determined by the sut fuses as shown in table 6-4 . notes: 1. if the rstdisbl fuse is pr ogrammed, this start-up time will be increased to 14ck + 4 ms to ensure programming mode can be entered. 2. the device is shipped with this option selected. 6.2.4 128 khz internal oscillator the 128 khz internal oscillator is a low power os cillator providing a clock of 128 khz. the fre- quency is nominal at 3 v and 25 c. this clock may be selected as the system clock by programming th e cksel fuses to 0110. when this clock source is sele cted, start-up times are determi ned by the sut fuses as shown in table 6-5 . note: 1. if the rstdisbl fuse is pr ogrammed, this start-up time will be increased to 14ck + 4 ms to ensure programming mode can be entered. table 6-3. internal calibrated rc o scillator operating modes cksel3..0 nominal frequency 0010 4.0 mhz 0100 8.0 mhz (1) table 6-4. start-up times for the internal calib rated rc oscillato r clock selection sut1..0 start-up time from power- down and power-save additional delay from reset recommended usage 00 6 ck 14ck (1) bod enabled 01 6 ck 14ck + 4 ms fast rising power 10 (2) 6 ck 14ck + 64 ms slowly rising power 11 reserved table 6-5. start-up times for the 128 khz internal oscillator sut1..0 start-up time from power- down and power-save additional delay from reset recommended usage 00 6 ck 14ck (1) bod enabled 01 6 ck 14ck + 4 ms fast rising power 10 6 ck 14ck + 64 ms slowly rising power 11 reserved
28 8246a?avr?11/09 attiny2313a/4313 6.2.5 crystal oscillator xtal1 and xtal2 are input and output, respectively, of an inverting amplifier which can be con- figured for use as an on-chip oscillator, as shown in figure 6-3 on page 28 . either a quartz crystal or a ceramic resonator may be used. c1 and c2 should always be equal for both crystals and resonators. the optimal value of the capacitors depends on the crystal or resonator in use, the amount of stray capacitance, and the electromagnetic noise of the env ironment. some initial guidelines for choosing capacitors for use with crystals are given in table 6-6 on page 28 . for ceramic resonators, the capacitor val- ues given by the manufacturer should be used. figure 6-3. crystal oscillator connections the oscillator can operate in three different mo des, each optimized for a specific frequency range. the oper ating mode is selected by the fuses cksel3..1 as shown in table 6-6 . note: 1. this option should not be used with crystals, only with ceramic resonators. the cksel0 fuse together with the sut1..0 fuses select the start-up times as shown in table 6-7 . table 6-6. crystal oscillator operating modes cksel3..1 frequency range (mhz) recommended range for capacitors c1 and c2 for use with crystals (pf) 100 (1) 0.4 - 0.9 ? 101 0.9 - 3.0 12 - 22 110 3.0 - 8.0 12 - 22 111 8.0 - 12 - 22 xtal2 xtal1 gnd c2 c1
29 8246a?avr?11/09 attiny2313a/4313 notes: 1. these options should only be used when not operating close to the maximum frequency of the device, and only if frequency stability at start- up is not important for the application. these options are not suitable for crystals. 2. these options are intended for use with cerami c resonators and will ensure frequency stability at start-up. they can also be used with crystals when not operating close to the maximum fre- quency of the device, and if frequency stability at start-up is not important for the application. 6.3 system clock prescaler the attiny2313a/4313 has a system clock pres caler, and the system clock can be divided by setting the ?clkpr ? clock prescale register? on page 30 . this feature can be used to decrease the system clock frequency and the power consumption when the requirement for pro- cessing power is low. this can be used with all cl ock source options, and it will affect the clock frequency of the cpu and all synchronous peripherals. clk i/o , clk cpu , and clk flash are divided by a factor as shown in table 6-8 on page 30 . 6.3.1 switching time when switching between prescaler settings, the system clock prescaler ensures that no glitches occurs in the clock system. it also ensu res that no intermediate frequency is higher than neither the clock frequency corresponding to th e previous setting, nor the clock frequency corre- sponding to the new setting. the ripple counter that implements the prescale r runs at the frequency of the undivided clock, which may be faster than the cpu' s clock frequency. hence, it is not possible to determine the state of the prescaler - even if it were readable, and the exact time it takes to switch from one clock division to the other cann ot be exactly predicted. from the time the clkps values are writ- ten, it takes between t1 + t2 and t1 + 2 * t2 before the new clock frequency is active. in this interval, two active clock edges are produced. here, t1 is the previous clock period, and t2 is the period corresponding to the new prescaler setting. table 6-7. start-up times for the crysta l oscillator clock selection cksel0 sut1..0 start-up time from power-down and power-save additional delay from reset (v cc = 5.0v) recommended usage 0 00 258 ck (1) 14ck + 4 ms ceramic resonator, fast rising power 0 01 258 ck (1) 14ck + 64 ms ceramic resonator, slowly rising power 010 1k ck (2) 14ck ceramic resonator, bod enabled 011 1k ck (2) 14ck + 4 ms ceramic resonator, fast rising power 100 1k ck (2) 14ck + 64 ms ceramic resonator, slowly rising power 1 01 16k ck 14ck crystal oscillator, bod enabled 1 10 16k ck 14ck + 4 ms crystal oscillator, fast rising power 1 11 16k ck 14ck + 64 ms crystal oscillator, slowly rising power
30 8246a?avr?11/09 attiny2313a/4313 6.4 clock output buffer the device can output the system clock on t he clko pin. to enable the output, the ckout fuse has to be programmed. this mode is suitable when the chip clock is used to drive other cir- cuits on the system. note that the clock will not be output duri ng reset and t hat the normal operation of the i/o pin will be overridden when the fuse is programmed. any clock source, including the internal rc oscillator, can be se lected when the clock is output on clko. if the system clock prescaler is used, it is the divided system clock that is output. 6.5 register description 6.5.1 osccal ? oscillato r calibration register ? bit 7 ? res: reserved bit this bit is reserved bit in attiny 2313a/4313 and it will always read zero. ? bits 6:0 ? cal[6:0]: oscillator calibration value writing the calibration byte to this address will trim the internal oscillator to remove process vari- ations from the oscillator frequency. this is done automatically during chip reset. when osccal is zero, the lowest available frequency is chosen. writing non-zero values to this regis- ter will increase the frequency of the internal oscillator. writing 0x7f to the register gives the highest available frequency. the calibrated oscillator is used to time eeprom and flash access. if eeprom or flash is written, do not calibrate to more than 10% above the nominal frequency. otherwise, the eeprom or flash write may fail. no te that the oscillator is intended for calibra tion to 8 mhz or 4 mhz. tuning to other values is not guaranteed, as indicated in table 6-8 below. to ensure stable operation of the mcu the calibration value should be changed in small steps. a variation in frequency of more than 2% from one cycle to the next can lead to unpredicatble behavior. changes in osccal should not exceed 0x20 for each calibration. it is required to ensure that the mcu is kept in reset during such changes in the clock frequency 6.5.2 clkpr ? clock prescale register ? bit 7 ? clkpce: clock prescaler change enable bit 76543210 0x31 (0x51) ? cal6 cal5 cal4 cal3 cal2 cal1 cal0 osccal read/write r r/w r/w r/w r/w r/w r/w r/w initial value 0 device specific calibration value table 6-8. internal rc oscillator frequency range osccal value typical lowest frequency with respect to nominal frequency typical highest frequency with respect to nominal frequency 0x00 50% 100% 0x3f 75% 150% 0x7f 100% 200% bit 7 6 5 4 3 2 1 0 0x26 (0x46) clkpce ? ? ? clkps3 clkps2 clkps1 clkps0 clkpr read/write r/w r r r r/w r/w r/w r/w initial value 0 0 0 0 see bit description
31 8246a?avr?11/09 attiny2313a/4313 the clkpce bit must be written to logic one to enable ch ange of the clkps bits. the clkpce bit is only updated when the other bits in cl kpr are simultaneously wr itten to zero. clkpce is cleared by hardware four cycles af ter it is written or when the clkps bits are written. rewriting the clkpce bit within this time-out period does ne ither extend the time-out period, nor clear the clkpce bit. ? bits 6:4 ? res: reserved bits these bits are reserved bits in the atti ny2313a/4313 and will always read as zero. ? bits 3:0 ? clkps3:0: clock prescaler select bits 3 - 0 these bits define the division factor between th e selected clock source and the internal system clock. these bits can be written run-time to va ry the clock frequency to suit the application requirements. as the divider divides the master cl ock input to the mcu, the speed of all synchro- nous peripherals is reduced when a division fact or is used. the division factors are given in table 6-9 on page 31 . to avoid unintentional changes of clock frequency, a special write procedure must be followed to change the clkps bits: 1. write the clock prescaler change enable (clkpce) bit to one and all other bits in clkpr to zero. 2. within four cycles, write the desired valu e to clkps while writin g a zero to clkpce. interrupts must be disabled when changing prescaler setting to make sure the write procedure is not interrupted.hee setting. the application softwa re must ensure that a sufficient division factor is chosen if the selected clock source has a higher frequency than the maximum frequency of the device at the present op erating conditions. the device is shipped with the ckdiv8 fuse programmed. table 6-9. clock prescaler select clkps3 clkps2 clkps1 clkps0 clock division factor 0000 1 0001 2 0010 4 0011 8 0100 16 0101 32 0110 64 0111 128 1000 256 1001 reserved 1010 reserved 1011 reserved 1100 reserved
32 8246a?avr?11/09 attiny2313a/4313 1101 reserved 1110 reserved 1111 reserved table 6-9. clock prescaler select (continued) clkps3 clkps2 clkps1 clkps0 clock division factor
33 8246a?avr?11/09 attiny2313a/4313 7. power management and sleep modes the high performance and industry leading code ef ficiency makes the avr microcontrollers an ideal choise for low power applications. in addition, sleep modes enable the application to shut down unused modules in the mcu, thereby saving power. the avr provides various sleep modes allowing the user to tailor the power consumption to the application?s requirements. 7.1 sleep modes figure 6-1 on page 24 presents the different clock systems and their distribution in attiny2313a/4313. the figure is helpful in selecting an appropriate sleep mode. table 7-1 shows the different sleep modes and their wake up sources. note: 1. for int0 and int1 , only level interrupt. to enter any of the three sleep modes, the se bit in mcucr must be written to logic one and a sleep instruction must be executed. the sm1..0 bits in the mcucr register select which sleep mode (idle, standby or power-down) will be activated by the sl eep instruction. see table 7-2 on page 36 for a summary. if an enabled interrupt occurs while the mcu is in a sleep mode, the mcu wakes up. the mcu is then halted for four cycles in addition to the st art-up time, executes the interrupt routine, and resumes execution from the instruction followi ng sleep. the contents of the register file and sram are unaltered when the device wakes up from sleep. if a reset occurs during sleep mode, the mcu wakes up and executes from the reset vector. note that if a level triggered interrupt is used for wake-up the changed level must be held for some time to wake up the mcu (and for the mc u to enter the interrupt service routine). see ?external interrupts? on page 48 for details. 7.1.1 idle mode when the sm1..0 bits are wri tten to 00, the sleep instruction makes the mcu enter idle mode, stopping the cpu but allowing analog comparator, usi, timer/counter, watchdog, and the interrupt system to continue operatin g. this sleep mode basically halts clk cpu and clk flash , while allowing the other clocks to run. idle mode enables the mcu to wake up from external triggered interrupts as well as internal ones like the timer overflow. if wake-up from the analog comparator interrupt is not required, the analog comparator can be powered down by setting the acd bit in ?acsr ? analog com- table 7-1. active clock domains and wake-up sources in different sleep modes sleep mode active clock domains oscillators wake-up sources clk cpu clk flash clk io main clock source enabled int0, int1 and pin change usi start condition spm/eeprom ready interrupt other i/o watchdog interrupt idle x x xxxxx power-down x (1) xx stand-by x x (1) xx
34 8246a?avr?11/09 attiny2313a/4313 parator control and status register? on page 167 . this will reduce power consumption in idle mode. 7.1.2 power-down mode when the sm1:0 bits are written to 01/11, th e sleep instruction makes the mcu enter power- down mode. in this mode, the os cillator is stopped, while the ex ternal interrupts, and the watch- dog continue operating (if enabled). only an external reset, a watchdog reset, a brown-out reset, an external level interrupt on int0 and int1, or a pin change interrupt can wake up the mcu. this sleep mode halts all generated clocks, allowing operation of asynchronous modules only. 7.1.3 standby mode when the sm1..0 bits are 10 and an external crystal/resonator clock option is selected, the sleep instruction makes the mcu enter standby mode. this mode is identical to power-down with the exception that the oscillator is kept running. fr om standby mode, the device wakes up in six clock cycles. 7.2 software bod disable when the brown-out detector (bod) is enabled by bodlevel fuses (see table 20-4 on page 179 ), the bod is actively monitoring the supply vo ltage during a sleep period. in some devices it is possible to save power by disabling the bod by software in power-down and stand-by sleep modes. the sleep mode power consumption will then be at the same level as when bod is glob- ally disabled by fuses. if bod is disabled by software, the bod function is turned off immediately after entering the sleep mode. upon wake-up from sleep, bod is automatically enabled again. this ensures safe operation in case the v cc level has dropped during the sleep period. when the bod has been disabled, the wake-up time from sleep mode will be approximately 60s to ensure that the bod is working corr ectly before the mcu continues executing code. bod disable is controlled by the bods (bod sleep) bit of bod control register, see ?bodcr ? brown-out detector control register? on page 37 . writing this bit to one turns off bod in power-down and stand-by, while writing a zero keeps the bod active. the default setting is zero, i.e. bod active. writing to the bods bit is controlled by a tim ed sequence and an enable bit, see ?bodcr ? brown-out detector control register? on page 37 . 7.3 power reduction register the power reduction register (prr), see ?prr ? power reduction register? on page 36 , pro- vides a method to reduce power consumption by stopping the clock to individual peripherals. when the clock for a peripheral is stopped then: ? the current state of the peripheral is frozen. ? the associated registers can not be read or written. ? resources used by the peri pheral will remain occupied. the peripheral should in most cases be disabled before stopping the clock. clearing the prr bit wakes up the peripheral and puts it in the same state as before shutdown.
35 8246a?avr?11/09 attiny2313a/4313 peripheral shutdown can be used in idle mode an d active mode to significantly reduce the over- all power consumption. see ?effect of power reduction? on page 203 for examples. in all other sleep modes, the clock is already stopped. 7.4 minimizing power consumption there are several issues to consider when tryi ng to minimize the power consumption in an avr controlled system. in general, sleep modes shoul d be used as much as possible, and the sleep mode should be selected so that as few as possi ble of the device?s func tions are operating. all functions not needed should be disabled. in parti cular, the following modules may need special consideration when trying to achieve th e lowest possible power consumption. 7.4.1 analog comparator when entering idle mode, the analog comparator should be disabled if not used. in the other sleep modes, the analog comparator is automatically disabled. however, if the analog compar- ator is set up to use the internal voltage refe rence as input, the analog comparator should be disabled in all sleep modes. otherwise, the in ternal voltage reference will be enabled, indepen- dent of sleep mode. see ?analog comparator? on page 167 for details on how to configure the analog comparator. 7.4.2 internal voltage reference the internal voltage re ference will be enabled when needed by the brown-out detection or the analog comparator. if these modules are disabled as described in the sections above, the inter- nal voltage reference will be disabled and it will not be consuming power. when turned on again, the user must allow the reference to start up before the output is used. if the reference is kept on in sleep mode, the output can be used immediately. see ?internal voltage reference? on page 41 for details on the start-up time. 7.4.3 brown-out detector if the brown-out detector is not needed in the ap plication, this module should be turned off. if the brown-out detector is enabled by the bodlevel fuses, it will be enabled in all sleep modes, and hence, always consume power. in the deeper sleep modes, this will contribute significantly to the total current consumption. see ?brown-out detection? on page 40 and ?software bod dis- able? on page 34 for details on how to configure the brown-out detector. 7.4.4 watchdog timer if the watchdog timer is not needed in the application, this module should be turned off. if the watchdog timer is enabled, it will be enabled in all sleep modes, and hence, always consume power. in the deeper slee p modes, this will contribute signific antly to the total current consump- tion. see ?interrupts? on page 47 for details on how to configure the watchdog timer. 7.4.5 port pins when entering a sleep mode, all port pins should be configured to use minimum power. the most important thing is then to ensure that no pins drive resistive loads. in sleep modes where the i/o clock (clk i/o ) is stopped, the input buffers of the device will be disabled. this ensures that no power is consumed by the input logic when not needed. in some cases, the input logic is needed for detecting wake-up conditions, and it will then be enabled. see the section ?digital input enable and sleep modes? on page 57 for details on which pins are enabled. if the input buffer is enabled and the input signal is left fl oating or has an analog signal level close to v cc /2, the input buffer will use excessive power.
36 8246a?avr?11/09 attiny2313a/4313 for analog input pins, the digital input buffer should be disabled at all times. an analog signal level close to v cc /2 on an input pin can cause significant current even in active mode. digital input buffers can be disabled by writing to the digital input disable register (didr). see ?didr ? digital input disable register? on page 168 for details. 7.5 register description 7.5.1 mcucr ? mcu control register the sleep mode control register contai ns control bits for power management. ? bits 6, 4 ? sm1..0: sleep mode select bits 1 and 0 these bits select between the four available sleep modes as shown in table 7-2 . note: standby mode is only recommended for use with external crystals or resonators. ? bit 5 ? se: sleep enable the se bit must be written to logic one to make the mcu enter the sleep mode when the sleep instruction is executed. to avoid the mcu entering the sleep mode unless it is the programmer?s purpose, it is recommended to wr ite the sleep enable (se) bit to one just before the execution of the sleep instruction and to clear it immediately af ter waking up. 7.5.2 prr ? power reduction register the power reduction register provides a met hod to reduce power consumption by allowing peripheral clock signals to be disabled. ? bits 7..4 ? res: reserved bits these bits are reserved and will always read zero. ? bit 3 ? prtim1: power reduction timer/counter1 writing a logic one to this bit shuts down the timer/counter1 module. when the timer/counter1 is enabled, operation will cont inue like before the shutdown. bit 76543210 0x35 (0x55) pud sm1 se sm0 isc11 isc10 isc01 isc00 mcucr read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 table 7-2. sleep mode select sm1 sm0 sleep mode 00idle 0 1 power-down 1 0 standby 1 1 power-down bit 7654 3 2 1 0 0x06 (0x26) ? ? ? ? prtim1 prtim0 prusi prusart prr read/write r r r r r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
37 8246a?avr?11/09 attiny2313a/4313 ? bit 2 ? prtim0: power reduction timer/counter0 writing a logic one to this bit shuts down the timer/counter0 module. when the timer/counter0 is enabled, operation will cont inue like before the shutdown. ? bit 1 ? prusi: power reduction usi writing a logic one to this bit shuts down t he usi by stopping the clock to the module. when waking up the usi again, the usi should be re initialized to ensure proper operation. ? bit 0 ? prusart: power reduction usart writing a logic one to this bit shuts down t he usi by stopping the clock to the module. when waking up the usi again, the usi should be re initialized to ensure proper operation. 7.5.3 bodcr ? brown-out detector control register the bod control register cont ains control bits for disabling the bod by software. ? bit 1 ? bods: bod sleep in order to disable bod during sleep the bods bit must be written to logic one. this is controlled by a timed sequence and the enable bit, bodse. first, both bods and bodse must be set to one. second, within four clock cycles, bods must be set to one and bodse must be set to zero. the bods bit is active thre e clock cycles after it is set. a sleep instruction must be exe- cuted while bods is acti ve in order to turn o ff the bod for the actual sleep mode. the bods bit is automatically cleared after three clock cycles. ? bit 0 ? bodse: bod sleep enable the bodse bit enables setting of bods control bit, as explained on bods bit description. bod disable is controlled by a timed sequence. bit 76543210 0x07 (0x27) ??????bodsbodsebodcr read/writerrrrrrr/wr/w initial value 0 0 0 0 0 0 0 0
38 8246a?avr?11/09 attiny2313a/4313 8. system control and reset 8.1 resetting the avr during reset, all i/o registers are set to their initial values, and the pr ogram starts execution from the reset vector. the instruction placed at the reset vector must be a rjmp ? relative jump ? instruction to the reset handling routine. if the program never enables an interrupt source, the interrupt vectors are not used, and regular program code can be placed at these locations. the circuit diagram in figure 8-1 shows the reset logic. electrical parameters of the reset circuitry are given in table 21-3 on page 198 . figure 8-1. reset logic the i/o ports of the avr are immediately reset to their initial state when a reset source goes active. this does not require any clock source to be running. after all reset sources have gone inactive, a delay counter is invoked, stretching the internal reset. this allows the power to reach a stable level before normal operation starts. the time-out period of the delay counter is defined by the user through the sut and cksel fuses. the dif- ferent selections for the delay period are presented in ?clock sources? on page 25 . mcu status register (mcusr) brown-out reset circuit bodlevel [2..0] delay counters cksel[3:0] ck timeout wdrf borf extrf porf data b u s clock generator spike filter pull-up resistor watchdog oscillator sut[1:0] power-on reset circuit rstdisbl
39 8246a?avr?11/09 attiny2313a/4313 8.2 reset sources the attiny2313a/4313 has four sources of reset: ? power-on reset. the mcu is reset when the supply voltage is below the power-on reset threshold (v pot ) ? external reset. the mcu is reset when a low level is pr esent on the reset pin for longer than the minimum pulse length when reset function is enabled ? watchdog reset. the mcu is reset when the watchdog timer period expires and the watchdog is enabled ? brown-out reset. the mcu is re set when the supply voltage v cc is below the brown-out reset threshold (v bot ) and the brown-out detector is enabled 8.2.1 power-on reset a power-on reset (por) pulse is generated by an on-chip detection circuit. the detection level is defined in ?system and reset characteristics? on page 198 . the por is activated whenever v cc is below the detection level. the por circuit can be used to trigger the start-up reset, as well as to detect a fa ilure in supply voltage. a power-on reset (por) circuit ensures that the device is reset from power-on. reaching the power-on reset threshold voltage invokes the delay counter, which determines how long the device is kept in reset after v cc rise. the reset signal is activated again, without any delay, when v cc decreases below the detection level. figure 8-2. mcu start-up, reset tied to v cc figure 8-3. mcu start-up, reset extended externally v reset time-out internal reset t tout v pot v rst cc reset time-out internal reset t tout v pot v rst v cc
40 8246a?avr?11/09 attiny2313a/4313 8.2.2 external reset an external reset is generated by a low level on the reset pin if enabled. reset pulses longer than the minimum pulse width (see ?system and reset characteristics? on page 198 ) will gener- ate a reset, even if the clock is not running. shorter pulses are not guaranteed to generate a reset. when the applied signal reaches the reset threshold voltage ? v rst ? on its positive edge, the delay counter starts the mcu after the time-out period ? t tout ? has expired. figure 8-4. external reset during operation 8.2.3 brown-out detection attiny2313a/4313 has an on-chip brown-out de tection (bod) circuit for monitoring the v cc level during operation by comparing it to a fixed trigger level. the trigger level for the bod can be selected by the bodlevel fu ses. the trigger level has a h ysteresis to ensure spike free brown-out detection. the hysteresis on the detection level should be interpreted as v bot+ = v bot + v hyst /2 and v bot- = v bot - v hyst /2. when the bod is enabled, and v cc decreases to a value below the trigger level (v bot- in figure 8-5 on page 40 ), the brown-out reset is i mmediately activated. when v cc increases above the trigger level (v bot+ in figure 8-5 on page 40 ), the delay counter starts the mcu after the time- out period t tout has expired. the bod circuit will only detect a drop in v cc if the voltage stays below the trigger level for lon- ger than t bod given in ?system and reset characteristics? on page 198 . figure 8-5. brown-out reset during operation cc v cc reset time-out internal reset v bot- v bot+ t tout
41 8246a?avr?11/09 attiny2313a/4313 8.2.4 watchdog reset when the watchdog times out, it will generate a short reset pulse of one ck cycle duration. on the falling edge of this pulse, the delay time r starts counting the time-out period t tout . see ?interrupts? on page 47 for details on operation of the watchdog timer. figure 8-6. watchdog reset du ring operation 8.3 internal voltage reference attiny2313a/4313 features an internal bandgap refe rence. this reference is used for brown-out detection, and it can be used as an input to the analog comparator. the bandgap voltage varies with supply voltage and temperature. 8.3.1 voltage reference enable signals and start-up time the voltage reference has a start-up time that may influence the way it should be used. the start-up time is given in ?system and reset characteristics? on page 198 . to save power, the reference is not always turned on. the reference is on during the following situations: 1. when the bod is enabled (by prog ramming the bodlevel [2..0] fuse). 2. when the internal reference is connected to the analog comparator (by setting the acbg bit in acsr). thus, when the bod is not enabled, after setting the acbg bit, the user must always allow the reference to start up before the output from t he analog comparator. to reduce power consump- tion in power-down mode, the user can avoid t he three conditions above to ensure that the reference is turned off before entering power-down mode. 8.4 watchdog timer the watchdog timer is clocked fr om an on-chip oscillator which runs at 128 khz. by controlling the watchdog timer prescaler, the watchdog re set interval can be adjusted as shown in table 8-3 on page 46 . the wdr ? watchdog reset ? instruction resets the watchdog timer. the watchdog timer is also reset when it is disabled and when a chip reset occurs. ten different clock cycle periods can be sele cted to determine the reset period. if the reset period expires without another watchdog reset, the attiny2313a/4313 resets and executes from the reset vector. for timing details on the watchdog reset, refer to table 8-3 on page 46 . ck cc
42 8246a?avr?11/09 attiny2313a/4313 the watchdog timer can also be configured to generate an interrupt instead of a reset. this can be very helpful when using the watchdog to wake-up from power-down. to prevent unintentional disabling of the watchdog or unintentional change of time-out period, two different safety levels are selected by the fuse wdton as shown in table 8-1 see ?timed sequences for changing the configuration of the watchdog timer? on page 42 for details. figure 8-7. watchdog timer 8.4.1 timed sequences for changing the configuration of the watchdog timer the sequence for changing configuration differs slightly between the two safety levels. separate procedures are described for each level. ? safety level 1 in this mode, the watchdog ti mer is initially disabled, but can be enabled by writing the wde bit to one without any restriction. a timed sequence is needed when disabling an enabled watchdog timer. to disable an enabl ed watchdog timer, the following procedure must be followed: a. in the same operation, write a logic one to wdce and wde. a logic one must be written to wde regardless of the previous value of the wde bit b. within the next four clock cycles, in the same operation, write the wde and wdp bits as desired, but with the wdce bit cleared ? safety level 2 in this mode, the watchdog ti mer is always enabled, and the wde bit will always read as one. a timed sequence is needed when changi ng the watchdog time-out period. to change the watchdog time-out, the following procedure must be followed: table 8-1. wdt configuration as a function of the fuse settings of wdton wdton safety level wdt initial state how to disable the wdt how to change time- out unprogrammed 1 disabled timed sequence no limitations programmed 2 enabled always enabled timed sequence osc/2k osc/4k osc/8k osc/16k osc/32k osc/64k osc/128k osc/256k osc/512k osc/1024k mcu reset watchdog prescaler 128 khz oscillator watchdog reset wdp0 wdp1 wdp2 wdp3 wde mux
43 8246a?avr?11/09 attiny2313a/4313 a. in the same operation, write a logica l one to wdce and wde. even though the wde always is set, the wde must be writ ten to one to start the timed sequence b. within the next four clock cycles, in th e same operation, write the wdp bits as desired, but with the wdce bit cleared. the value written to the wde bit is irrelevant 8.4.2 code example the following code example shows one assembly and one c function for turning off the wdt. the example assumes that interrupts are controlle d (e.g., by disabling interrupts globally) so that no interrupts will occur during execution of these functions. note: 1. see ?code examples? on page 6. assembly code example (1) wdt_off: wdr ; clear wdrf in mcusr ldi r16, (0< 44 8246a?avr?11/09 attiny2313a/4313 8.5 register description 8.5.1 mcusr ? mcu status register the mcu status register provides information on which reset source caused an mcu reset. ? bits 7..4 ? res: reserved bits these bits are reserved bits in the atti ny2313a/4313 and will always read as zero. ? bit 3 ? wdrf: watchdog reset flag this bit is set if a watchdog re set occurs. the bit is reset by a power-on reset, or by writing a logic zero to the flag. ? bit 2 ? borf: brown-out reset flag this bit is set if a brown-out reset occurs. the bi t is reset by a power-on reset, or by writing a logic zero to the flag. ? bit 1 ? extrf: external reset flag this bit is set if an external reset occurs. the bit is reset by a power-on reset, or by writing a logic zero to the flag. ? bit 0 ? porf: power-on reset flag this bit is set if a power-on reset occurs. the bit is reset only by writing a logic zero to the flag. to make use of the reset flags to identify a rese t condition, the user should read and then reset the mcusr as early as possible in the program. if the register is cleared before another reset occurs, the source of the reset can be found by examining the reset flags. 8.5.2 wdtcsr ? watchdog timer control and status register ? bit 7 ? wdif: watchdog timeout interrupt flag this bit is set when a time-out occurs in the watchdog timer and the watchdog timer is config- ured for interrupt. wdif is cleared by hardw are when executing the corresponding interrupt handling vector. alternatively, wdif is cleared by writing a logic one to the flag. when the i-bit in sreg and wdie are set, the watchdog time-out interrupt is executed. ? bit 6 ? wdie: watchdog timeout interrupt enable when this bit is written to one, wde is cleared, and the i-bit in the status register is set, the watchdog time-out interrupt is enabled. in this mode the corresponding interrupt is executed instead of a reset if a timeout in the watchdog timer occurs. if wde is set, wdie is automatically cleared by hardware when a time-out occurs. this is useful for keeping the watchdog reset security while usin g the interrupt. after the wdie bit is cleared, bit 76543210 0x34 (0x54) ? ? ? ? wdrf borf extrf porf mcusr read/write rrrrr/wr/wr/wr/w initial value 0 0 0 0 see bit description bit 76543210 0x21 (0x41) wdif wdie wdp3 wdce wde wdp2 wdp1 wdp0 wdtcsr read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 x 0 0 0
45 8246a?avr?11/09 attiny2313a/4313 the next time-out will generate a reset. to av oid the watchdog reset, wdie must be set after each interrupt. ? bit 4 ? wdce: watchdog change enable this bit must be set when the wde bit is writte n to logic zero. otherwis e, the watchdog will not be disabled. once written to one , hardware will clear this bit after four clock cycles. see the description of the wde bit for a watchdog disabl e procedure. this bit must also be set when changing the prescaler bits. see ?timed sequences for changing the configuration of the watchdog timer? on page 42 . ? bit 3 ? wde: watchdog enable when the wde is written to logic one, the watchdog timer is enabled, and if the wde is written to logic zero, the watchdog timer function is di sabled. wde can only be cleared if the wdce bit has logic level one. to disable an enabled watchdog timer, the following procedure must be followed: 1. in the same operation, write a logic one to wdce and wde. a logic one must be writ- ten to wde even though it is set to one before the disable operation starts. 2. within the next four clock cycles, write a logic 0 to wde. this disables the watchdog. in safety level 2, it is not possible to disable the watchdog timer, even with the algorithm described above. see ?timed sequences for changing the configuration of the watchdog timer? on page 42 . in safety level 1, wde is overridden by wdrf in mcusr. see ?mcusr ? mcu status regis- ter? on page 44 for description of wdrf. this means that wde is always set when wdrf is set. to clear wde, wdrf must be cleared before disabling the watchdog with the procedure described above. this feature ensur es multiple resets during c onditions causing failure, and a safe start-up a fter the failure. note: if the watchdog timer is not going to be used in the application, it is im portant to go through a watchdog disable procedure in the initialization of the device. if the watchdog is accidentally enabled, for example by a runaway pointer or brown- out condition, the device will be reset, which in turn will lead to a new watchdog reset. to avoid this situation, the application software should always clear the wdrf flag and the wde c ontrol bit in the in itialization routine. ? bits 5, 2..0 ? wdp3..0: watchdog timer prescaler 3, 2, 1, and 0 the wdp3..0 bits determine the watchdog ti mer prescaling when the watchdog timer is enabled. the different prescaling values and th eir corresponding timeout periods are shown in table 8-3 on page 46 . table 8-2. watchdog timer configuration wde wdie watchdog timer state action on time-out 0 0 stopped none 0 1 running interrupt 1 0 running reset 1 1 running interrupt
46 8246a?avr?11/09 attiny2313a/4313 note: 1. if selected, one of the valid settings below 0b1010 will be used. table 8-3. watchdog timer prescale select wdp3 wdp2 wdp1 wdp0 number of wdt oscillator cycles typical time-out at v cc = 5.0v 0 0 0 0 2k cycles 16 ms 0 0 0 1 4k cycles 32 ms 0 0 1 0 8k cycles 64 ms 0 0 1 1 16k cycles 0.125 s 0 1 0 0 32k cycles 0.25 s 0 1 0 1 64k cycles 0.5 s 0 1 1 0 128k cycles 1.0 s 0 1 1 1 256k cycles 2.0 s 1 0 0 0 512k cycles 4.0 s 1 0 0 1 1024k cycles 8.0 s 1010 reserved (1) 1011 1100 1101 1110 1111
47 8246a?avr?11/09 attiny2313a/4313 9. interrupts this section describes the specifics of the inte rrupt handling as performed in attiny2313a/4313. for a general explanation of the avr interrupt handling, refer to ?reset and interrupt handling? on page 12 . 9.1 interrupt vectors the interrupt vectors of attiny2313a/4313 are described in table 9-1 below in case the program never enab les an interrupt source, the interrupt vectors will not be used and, consequently, regular program code can be placed at these locations. table 9-1. reset and interrupt vectors vector no. program address label interrupt source 1 0x0000 reset external pin, power-o n reset, brown-out reset, and watchdog reset 2 0x0001 int0 external interrupt request 0 3 0x0002 int1 external interrupt request 1 4 0x0003 timer1 capt timer/counter1 capture event 5 0x0004 timer1 compa timer/counter1 compare match a 6 0x0005 timer1 ovf timer/counter1 overflow 7 0x0006 timer0 ovf timer/counter0 overflow 8 0x0007 usart0, rx usart0, rx complete 9 0x0008 usart0, udre usart0 data register empty 10 0x0009 usart0, tx usart0, tx complete 11 0x000a analog comp analog comparator 12 0x000b pcint0 pin change interrupt request 0 13 0x000c timer1 compb timer/counter1 compare match b 14 0x000d timer0 compa timer/counter0 compare match a 15 0x000e timer0 compb timer/counter0 compare match b 16 0x000f usi start usi start condition 17 0x0010 usi overflow usi overflow 18 0x0011 ee ready eeprom ready 19 0x0012 wdt overflow watchdog timer overflow 20 0x0013 pcint1 pin change interrupt request 1 21 0x0014 pcint2 pin change interrupt request 2
48 8246a?avr?11/09 attiny2313a/4313 the most typical and general setup for the interrupt vector addresses in attiny2313a/4313 shown below: address labels code comments 0x0000 rjmp reset ; reset handler 0x0001 rjmp int0 ; external interrupt0 handler 0x0002 rjmp int1 ; external interrupt1 handler 0x0003 rjmp tim1_capt ; timer1 capture handler 0x0004 rjmp tim1_compa ; timer1 comparea handler 0x0005 rjmp tim1_ovf ; timer1 overflow handler 0x0006 rjmp tim0_ovf ; timer0 overflow handler 0x0007 rjmp usart0_rxc ; usart0 rx complete handler 0x0008 rjmp usart0_dre ; usart0,udr empty handler 0x0009 rjmp usart0_txc ; usart0 tx complete handler 0x000a rjmp ana_comp ; analog comparator handler 0x000b rjmp pcint0 ; pcint0 handler 0x000c rjmp timer1_compb ; timer1 compare b handler 0x000d rjmp timer0_compa ; timer0 compare a handler 0x000e rjmp timer0_compb ; timer0 compare b handler 0x000f rjmp usi_start ; usi start handler 0x0010 rjmp usi_overflow ; usi overflow handler 0x0011 rjmp ee_ready ; eeprom ready handler 0x0012 rjmp wdt_overflow ; watchdog overflow handler 0x0013 rjmp pcint1 ; pcint1 handler 0x0014 rjmp pcint2 ; pcint2 handler ; 0x0013 reset: ldi r16, low(ramend); main program start 0x0014 out spl,r16 set stack pointer to top of ram 0x0015 sei ; enable interrupts 0x0016 xxx ... ... ... ... 9.2 external interrupts external interrupts are triggered by the int0 or int1 pin or any of the pcint17..0 pins. observe that, if enabled, the interrupts will trigger even if the int0, int1 or pcint17..0 pins are config- ured as outputs. this feature provides a way of generating a software interrupt. pin change 0 interrupts pci0 will trigger if any enabled pcint7..0 pin toggle s. pin change 1 interrupts pci1 will trigger if any enabled pcint10 ..8 pin toggles. pin change 2 inte rrupts pci2 will trigger, if any enabled pcint17..11 pin toggles . the pcmsk0, pcmsk1, and pcmsk2 registers control which pins contribute to the pin change interr upts. pin change interrup ts on pcint17..0 are detected asynchronously, which means that these interrupts c an be used for waking the part also from sleep modes other than idle mode. the int0 and int1 interrupts can be triggered by a falling or rising edge or a low level. this is set up as shown in ?mcucr ? mcu control re gister? on page 50 . when the int0 or int1 inter- rupt is enabled and configured as level triggered, the interrupt w ill trigger as long as the pin is held low. note that recognition of falling or ri sing edge interrupts on int0 or int1 requires the presence of an i/o clock, as described in ?clock sources? on page 25 .
49 8246a?avr?11/09 attiny2313a/4313 9.2.1 low level interrupt a low level interrupt on int0 or int1 is detect ed asynchronously. this means that the interrupt source can be used for waking the part also from sleep modes other than idle (the i/o clock is halted in all sleep modes except idle). note that if a level triggered interrupt is used for wake-up from power-down, the required level must be held long enough for the mcu to complete t he wake-up to trigger the level interrupt. if the level disappears before the end of the start-up ti me, the mcu will still wake up, but no inter- rupt will be generated. the start-up time is defined by the sut and cksel fuses, as described in ?clock system? on page 24 . if the low level on the interrupt pin is removed before the device has woken up then program execution will not be diverted to the interrupt service ro utine but continue from the instruction fol- lowing the sleep command. 9.2.2 pin change interrupt timing a timing example of a pin change interrupt is shown in figure 9-1 . figure 9-1. timing of pin change interrupts clk pcint(0) pin_lat pin_sync pcint_in_(0) pcint_syn pcint_setflag pcif pcint(0) pin_sync pcint_syn pin_lat d q le pcint_setflag pcif clk clk pcint(0) in pcmsk(x) pcint_in_(0) 0 x
50 8246a?avr?11/09 attiny2313a/4313 9.3 register description 9.3.1 mcucr ? mcu control register the external interrupt control register contai ns control bits for interrupt sense control. ? bit 3, 2 ? isc11, isc10: interrupt sense control 1 bit 1 and bit 0 the external interrupt 1 is activated by the exte rnal pin int1 if the sr eg i-flag and the corre- sponding interrupt mask are set. the level and edges on the external int1 pin that activate the interrupt are defined in table 9-2 . the value on the int1 pin is sampled before detecting edges. if edge or toggle interrupt is selected, pulses that last longer than one clock period will generate an interrupt. shorter pulses are not guaranteed to generate an interrupt. if low level interrupt is selected, the low level must be held until the completion of the currently executing instruction to generate an interrupt ? bits 1, 0 ? isc01, isc00: interrupt sense control 0 bit 1 and bit 0 the external interrupt 0 is activated by the exte rnal pin int0 if the sr eg i-flag and the corre- sponding interrupt mask are set. the level and edges on the external int0 pin that activate the interrupt are defined in table 9-3 . the value on the int0 pin is sampled before detecting edges. if edge or toggle interrupt is selected, pulses that last longer than one clock period will generate an interrupt. shorter pulses are not guaranteed to generate an interrupt. if low level interrupt is selected, the low level must be held until the completion of the currently executing instruction to generate an interrupt. 9.3.2 gimsk ? general interrupt mask register bit 76543210 0x35 (0x55) pud sm1 se sm0 isc11 isc10 isc01 isc00 mcucr read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 table 9-2. interrupt 1 sense control isc11 isc10 description 0 0 the low level of int1 generates an interrupt request. 0 1 any logical change on int1 generates an interrupt request. 1 0 the falling edge of int1 generates an interrupt request. 1 1 the rising edge of int1 generates an interrupt request. table 9-3. interrupt 0 sense control isc01 isc00 description 0 0 the low level of int0 generates an interrupt request. 0 1 any logical change on int0 generates an interrupt request. 1 0 the falling edge of int0 generates an interrupt request. 1 1 the rising edge of int0 generates an interrupt request. bit 76543210 0x3b (0x5b) int1 int0 pcie0 pcie2 pcie1 ? ? ? gimsk read/write r/w r/w r/w r/w r/w r r r initial value 0 0 0 0 0 0 0 0
51 8246a?avr?11/09 attiny2313a/4313 ? bits 2..0 ? res: reserved bits these bits are reserved and will always read as zero. ? bit 7 ? int1: external interrupt request 1 enable when the int1 bit is set (one) and the i-bit in th e status register (sreg) is set (one), the exter- nal pin interrupt is enabled. the interrupt sense control bits (isc11 and isc10) in the external interrupt control register a (eic ra) define whether the external interrupt is activated on rising and/or falling edge of the int1 pin or level sens ed. activity on the pin will cause an interrupt request even if int1 is configured as an output. the corresponding interrupt of external interrupt request 1 is executed from the int1 interrupt vector. ? bit 6 ? int0: external interrupt request 0 enable when the int0 bit is set (one) and the i-bit in th e status register (sreg) is set (one), the exter- nal pin interrupt is enabled. the interrupt sense control bits (isc01 and isc00) in the external interrupt control register a (eic ra) define whether the external interrupt is activated on rising and/or falling edge of the int0 pin or level sens ed. activity on the pin will cause an interrupt request even if int0 is configured as an output. the corresponding interrupt of external interrupt request 0 is executed from the int0 interrupt vector. ? bit 5 ? pcie0: pin change interrupt enable 0 when the pcie0 bit is set (one) and the i-bit in the status register (sreg) is set (one), pin change interrupt 0 is enabled. any change on any enabl ed pcint7..0 pin will cause an interrupt. the corresponding interrupt of pin change interrupt request is executed from the pci0 inter- rupt vector. pcint7..0 pins are enabled individually by the pcmsk0 register. ? bit 4 ? pcie2: pin change interrupt enable 2 when the pcie2 bit is set (one) and the i-bit in the status register (sreg) is set (one), pin change interrupt 2 is enabled. any change on any e nabled pcint17..11 pin will cause an inter- rupt. the corresponding interrupt of pin change interrupt request is executed from the pci2 interrupt vector. pcint17..11 pins are enab led individually by the pcmsk2 register. ? bit 3 ? pcie1: pin change interrupt enable 1 when the pcie1 bit is set (one) and the i-bit in the status register (sreg) is set (one), pin change interrupt 1 is enabled. any change on any enabled pcin t10..8 pin will cause an inter- rupt. the corresponding interrupt of pin change interrupt request is executed from the pci1 interrupt vector. pcint10..8 pins are enabl ed individually by the pcmsk1 register. 9.3.3 gifr ? general in terrupt flag register ? bits 2..0 ? res: reserved bits these bits are reserved and will always read as zero. ? bit 7 ? intf1: external interrupt flag 1 when an edge or logic change on the int1 pin triggers an interrupt request, intf1 becomes set (one). if the i-bit in sreg and the int1 bit in gimsk are set (o ne), the mcu will jump to the cor- responding interrupt vector. the flag is cleared when the interrupt routine is executed. bit 76543210 0x3a (0x5a) intf1 intf0 pcif0 pcif2 pcif1 ? ? ? gifr read/write r/w r/w r/w r/w r/w r r r initial value 0 0 0 0 0 0 0 0
52 8246a?avr?11/09 attiny2313a/4313 alternatively, the flag can be clea red by writing a logical one to it. this flag is always cleared when int1 is configured as a level interrupt. ? bit 6 ? intf0: external interrupt flag 0 when an edge or logic change on the int0 pin triggers an interrupt request, intf0 becomes set (one). if the i-bit in sreg and the int0 bit in gimsk are set (o ne), the mcu will jump to the cor- responding interrupt vector. the flag is cleared when the interrupt routine is executed. alternatively, the flag can be clea red by writing a logical one to it. this flag is always cleared when int0 is configured as a level interrupt. ? bit 5 ? pcif0: pin change interrupt flag 0 when a logic change on any pcint7..0 pin triggers an interrupt request, pcif becomes set (one). if the i-bit in sreg and the pcie0 bit in gimsk are set (one), the mcu will jump to the corresponding interrupt vector. the flag is cleared when the interrupt rout ine is executed. alter- natively, the flag can be cleared by writing a logical one to it. ? bit 4 ? pcif2: pin change interrupt flag 2 when a logic change on any pcint17..11 pin triggers an interrupt request, pcif2 becomes set (one). if the i-bit in sreg and the pcie2 bit in gimsk are set (one), the mcu will jump to the corresponding interrupt vector. the flag is cleared when the interrupt rout ine is executed. alter- natively, the flag can be cleared by writing a logical one to it. ? bit 3 ? pcif1: pin change interrupt flag 1 when a logic change on any pcint10..8 pin triggers an interrupt request, pcif1 becomes set (one). if the i-bit in sreg and the pcie1 bit in gimsk are set (one), the mcu will jump to the corresponding interrupt vector. the flag is cleared when the interrupt rout ine is executed. alter- natively, the flag can be cleared by writing a logical one to it. 9.3.4 pcmsk2 ? pin change mask register 2 ? bit 7 ? res: reserved bit these bits are reserved and will always read as zero. ? bits 6..0 ? pcint17..11: pin change enable mask 17..11 each pcint17..11 bit selects whether pin change interrupt is enabled on the corresponding i/o pin. if pcint17..11 is set and the pcie1 bit in gimsk is set, pin change interrupt is enabled on the corresponding i/o pin. if pc int17..11 is cleared, pin change interrupt on the corresponding i/o pin is disabled. 9.3.5 pcmsk1 ? pin change mask register 1 bit 7654 3 2 1 0 0x05 (0x25) ? pcint17 pcint16 pcint15 pcint14 pcint13 pcint12 pcint11 pcmsk2 read/write r r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 bit 7 6 5 4 3 2 1 0 0x04 (0x24) ? ? ? ? ? pcint10 pcint9 pcint8 pcmsk1 read/write r r r r r r/w r/w r/w initial value 0 0 0 0 0 0 0 0
53 8246a?avr?11/09 attiny2313a/4313 ? bits 7:3 ? res: reserved bits these bits are reserved and will always read as zero. ? bits 2..0 ? pcint10..8: pin change enable mask 10..8 each pcint10..8 bit selects whether pin change interrupt is enabled on the corresponding i/o pin. if pcint10..8 is set and the pcie1 bit in gimsk is set, pin change interrupt is enabled on the corresponding i/o pin. if pcint10..8 is cleared, pin change interrupt on the corresponding i/o pin is disabled. 9.3.6 pcmsk0 ? pin change mask register 0 ? bits 7..0 ? pcint7..0: pin change enable mask 7..0 each pcint7..0 bit selects whether pin change interrupt is enabled on the corresponding i/o pin. if pcint7..0 is set and the pcie0 bit in gim sk is set, pin change interrupt is enabled on the corresponding i/o pin. if pcint7 ..0 is cleared, pin change interrupt on the corresponding i/o pin is disabled. bit 76543210 0x20 (0x40) pcint7 pcint6 pcint5 pcint4 pcint3 pcint2 pcint1 pcint0 pcmsk0 read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
54 8246a?avr?11/09 attiny2313a/4313 10. i/o-ports all avr ports have true read-modi fy-write functionality when used as general digital i/o ports. this means that the dire ction of one port pin can be chan ged without unintentionally changing the direction of any other pin with the sbi and cbi instructions. the same applies when chang- ing drive value (if configured as output) or enab ling/disabling of pull-up resistors (if configured as input). each output buffer has symmetrical drive characteristics with both high sink and source capability. the pin driver is stro ng enough to drive led displays directly. all port pins have indi- vidually selectable pull-up resistors with a suppl y-voltage invariant resistance. all i/o pins have protection diodes to both v cc and ground as indicated in figure 10-1 on page 54 . see ?electri- cal characteristics? on page 195 for a complete list of parameters. figure 10-1. i/o pin equivalent schematic all registers and bit references in this section are written in general form. a lower case ?x? repre- sents the numbering letter for the port, and a lower case ?n? represents the bit number. however, when using the register or bit defines in a progr am, the precise form must be used. for example, portb3 for bit no. 3 in port b, here document ed generally as portxn. the physical i/o regis- ters and bit locations are listed in ?register description? on page 68 . three i/o memory address locations are allocated for each port, one each for the data register ? portx, data direction register ? ddrx, and th e port input pins ? pi nx. the port input pins i/o location is read only, while th e data register and the data direction register are read/write. however, writing a logic one to a bit in the pinx register, will result in a toggle in the correspond- ing bit in the data register. in addition, the pull-up disable ? pud bit in mcuc r disables the pull-up function for all pins in all ports when set. using the i/o port as general digital i/o is described in ?ports as general digital i/o? on page 55 . most port pins are multiplexed with alternate functions for the peripheral features on the device. how each alternate function interferes with the port pin is described in ?alternate port functions? on page 59 . refer to the individual module sectio ns for a full description of the alter- nate functions. note that enabling the alternate function of some of the port pins does not affect the use of the other pins in the port as general digital i/o. c pin logic r pu see figure "general digital i/o" for details pxn
55 8246a?avr?11/09 attiny2313a/4313 10.1 ports as general digital i/o the ports are bi-directional i/o ports with optional internal pull-ups. figure 10-2 shows a func- tional description of one i/o-port pin, here generically called pxn. figure 10-2. general digital i/o (1) note: 1. wrx, wpx, wdx, rrx, rpx, and rdx are co mmon to all pins within the same port. clk i/o , sleep, and pud are common to all ports. 10.1.1 configuring the pin each port pin consists of three register bits: ddxn, portxn, and pinxn. as shown in ?register description? on page 68 , the ddxn bits are accessed at th e ddrx i/o address, the portxn bits at the portx i/o address, and the pi nxn bits at the pinx i/o address. the ddxn bit in the ddrx register selects the direct ion of this pin. if ddxn is written logic one, pxn is configured as an output pin. if ddxn is written logic ze ro, pxn is configured as an input pin. if portxn is written logic one w hen the pin is configured as an i nput pin, the pull-up resistor is activated. to switch the pull-up re sistor off, portxn has to be wr itten logic zero or the pin has to be configured as an output pin. the port pins are tri-stated when reset condition becomes active, even if no clocks are running. if portxn is written logic one when the pin is configured as an ou tput pin, the port pin is driven high (one). if portxn is writte n logic zero when the pin is config ured as an output pin, the port pin is driven low (zero). clk rpx rrx rdx wdx pud synchronizer wdx: write ddrx wrx: write portx rrx: read portx register rpx: read portx pin pud: pullup disable clk i/o : i/o clock rdx: read ddrx d l q q reset reset q q d q q d clr portxn q q d clr ddxn pinxn data b u s sleep sleep: sleep control pxn i/o wpx 0 1 wrx wpx: write pinx register
56 8246a?avr?11/09 attiny2313a/4313 10.1.2 toggling the pin writing a logic one to pinxn toggles the value of portxn, independent on the value of ddrxn. note that the sbi instruction can be us ed to toggle one single bit in a port. 10.1.3 switching between input and output when switching between tri-state ({ddxn, port xn} = 0b00) and output high ({ddxn, portxn} = 0b11), an intermediate state with either pull-up enabled {ddxn, portxn} = 0b01) or output low ({ddxn, portxn} = 0b10) must occur. norma lly, the pull-up enabled state is fully accept- able, as a high-impedant enviro nment will not notice the differenc e between a strong high driver and a pull-up. if this is not the case, the pud bit in the mcucr register can be set to disable all pull-ups in all ports. switching between input with pull-up and output low generates the same problem. the user must use either the tri-state ({ddxn, portxn} = 0b00) or the output high state ({ddxn, portxn} = 0b10) as an intermediate step. table 10-1 summarizes the control signals for the pin value. 10.1.4 reading the pin value independent of the setting of data direction bit ddxn, the port pin can be read through the pinxn register bit. as shown in figure 10-2 on page 55 , the pinxn register bit and the preced- ing latch constitute a synchronizer. this is ne eded to avoid metastability if the physical pin changes value near the edge of the internal clock, but it also introduces a delay. figure 10-3 shows a timing diagram of the synchronization when reading an externally applied pin value. the maximum and minimum propagation delays are denoted t pd,max and t pd,min respectively. figure 10-3. synchronization when reading an externally applied pin value table 10-1. port pin configurations ddxn portxn pud (in mcucr) i/o pull-up comment 0 0 x input no tri-state (hi-z) 0 1 0 input yes pxn will source current if ext. pulled low 0 1 1 input no tri-state (hi-z) 1 0 x output no output low (sink) 1 1 x output no output high (source) xxx in r17, pinx 0x00 0xff instructions sync latch pinxn r17 xxx system clk t pd, max t pd, min
57 8246a?avr?11/09 attiny2313a/4313 consider the clock period starting shortly after the first falling edge of the system cl ock. the latch is closed when the clock is low, and goes transpa rent when the clock is high, as indicated by the shaded region of the ?sync latch? signal. the signal value is latched when the system clock goes low. it is clocked into the pinxn register at the succeeding positive clock edge. as indi- cated by the two arrows tpd,max and tpd,min, a single signal tr ansition on the pin will be delayed between ? and 1? system clock period depending upon the time of assertion. when reading back a software assigned pin value, a nop instruction must be inserted as indi- cated in figure 10-4 on page 57 . the out instruction sets the ?sync latch? signal at the positive edge of the clock. in this case, the delay tpd through the synchronizer is one system clock period. figure 10-4. synchronization when reading a software assigned pin value 10.1.5 digital input enable and sleep modes as shown in figure 10-2 on page 55 , the digital input signal can be clamped to ground at the input of the schmitt-trigger. th e signal denoted sleep in the fi gure, is set by the mcu sleep controller in power-down and standby modes to avoid high power consumption if some input signals are left floating, or have an analog signal level close to v cc /2. sleep is overridden for port pins enabled as ex ternal interrupt pins. if the external interrupt request is not e nabled, sleep is active also for these pi ns. sleep is also over ridden by various other alternate functions as described in ?alternate port functions? on page 59 . if a logic high level (?one?) is present on an asyn chronous external interrupt pin configured as ?interrupt on rising edge, falling edge, or any logic change on pin? while the external interrupt is not enabled, the corresponding external interrupt flag will be set when resuming from the above mentioned sleep mode, as the clamping in these sleep mode produces the requested logic change. 10.1.6 unconnected pins if some pins are unused, it is recommended to ensure that these pins have a defined level. even though most of the digital inputs are disabled in the deep sleep modes as described above, float- ing inputs should be avoided to reduce current consumption in all other modes where the digital inputs are enabled (reset, active mode and idle mode). the simplest method to ensure a defined level of an unused pin, is to enable the internal pull-up. in this case, the pull-up will be disabled during reset. if low po wer consumption during reset is out portx, r16 nop in r17, pinx 0xff 0x00 0xff system clk r16 instructions sync latch pinxn r17 t pd
58 8246a?avr?11/09 attiny2313a/4313 important, it is recommended to use an external pull-up or pulldown. connecting unused pins directly to v cc or gnd is not recommended, since this ma y cause excessive curr ents if the pin is accidentally configured as an output. 10.1.7 program examples the following code example shows how to set port a pins 0 and 1 high, 2 and 3 low, and define the port pins from 4 to 7 as input with a pull-up assigned to port pin 4. the resulting pin values are read back again, but as previously discussed, a nop instruction is included to be able to read back the value recently assigned to some of the pins. note: two temporary registers are used to minimize the time from pull-ups are set on pins 0, 1 and 4, until the direction bits are correctly set, defining bit 2 and 3 as low and redefining bits 0 and 1 as strong high drivers. note: 1. see ?code examples? on page 6. assembly code example ... ; define pull-ups and set outputs high ; define directions for port pins ldi r16,(1< 59 8246a?avr?11/09 attiny2313a/4313 10.2 alternate port functions most port pins have alternat e functions in addition to being general digital i/os. in figure 10-5 below is shown how the port pin c ontrol signals from the simplified figure 10-2 on page 55 can be overridden by alternate functions. figure 10-5. alternate port functions (1) note: 1. wrx, wpx, wdx, rrx, rpx, and rdx are co mmon to all pins within the same port. clk i/o , sleep, and pud are common to all ports. all other signals are unique for each pin. the illustration in the figure above serves as a generic description applicable to all port pins in the avr microcontroller family. some overriding signals may not be present in all port pins. clk rpx rrx wrx rdx wdx pud synchronizer wdx: write ddrx wrx: write portx rrx: read portx register rpx: read portx pin pud: pullup disable clk i/o : i/o clock rdx: read ddrx d l q q set clr 0 1 0 1 0 1 dixn aioxn dieoexn pvovxn pvoexn ddovxn ddoexn puoexn puovxn puoexn: pxn pull-up override enable puovxn: pxn pull-up override value ddoexn: pxn data direction override enable ddovxn: pxn data direction override value pvoexn: pxn port value override enable pvovxn: pxn port value override value dixn: digital input pin n on portx aioxn: analog input/output pin n on portx reset reset q q d clr q q d clr q q d clr pinxn portxn ddxn data b u s 0 1 dieovxn sleep dieoexn: pxn digital input-enable override enable dieovxn: pxn digital input-enable override value sleep: sleep control pxn i/o 0 1 ptoexn ptoexn: pxn, port toggle override enable wpx: write pinx wpx
60 8246a?avr?11/09 attiny2313a/4313 table 10-2 summarizes the function of the overriding signals. the pin and port indexes from fig- ure 10-5 are not shown in the succeeding tables. the overriding signals are generated internally in the modules having the alternate function. the following subsections shortly describe the alte rnate functions for each port, and relate the overriding signals to the alternate function. refer to the alternate function description for further details. table 10-2. generic description of overriding signals for alternate functions signal name full name description puoe pull-up override enable if this signal is set, the pull-up enable is controlled by the puov signal. if this signal is cleared, the pull-up is enabled when {ddxn, portxn, pud} = 0b010. puov pull-up override value if puoe is set, the pull-up is enabled/disabled when puov is set/cleared, regardless of t he setting of the ddxn, portxn, and pud register bits. ddoe data direction override enable if this signal is set, the output driver enable is controlled by the ddov signal. if this signal is cl eared, the output driver is enabled by the ddxn register bit. ddov data direction override value if ddoe is set, the output dr iver is enabled/disabled when ddov is set/cleared, regardless of the setting of the ddxn register bit. pvoe port value override enable if this signal is set and the outp ut driver is enabled, the port value is controlled by the pvov si gnal. if pvoe is cleared, and the output driver is enabled, the port value is controlled by the portxn register bit. pvov port value override value if pvoe is set, the port value is set to pvov, regardless of the setting of the portxn register bit. ptoe port toggle override enable if ptoe is set, the portxn register bit is inverted. dieoe digital input enable override enable if this bit is set, the digital input enable is controlled by the dieov signal. if this signal is cleared, the digital input enable is determined by mcu state (normal mode, sleep mode). dieov digital input enable override value if dieoe is set, the digital input is enabled/disabled when dieov is set/cleared, regardl ess of the mcu state (normal mode, sleep mode). di digital input this is the digital input to alte rnate functions. in the figure, the signal is connected to the out put of the schmitt-trigger but before the synchronizer. unless the digital input is used as a clock source, the module with the alternate function will use its own synchronizer. aio analog input/output this is the analog input/output to /from alternate functions. the signal is connected directly to the pad, and can be used bi- directionally.
61 8246a?avr?11/09 attiny2313a/4313 10.2.1 alternate functions of port a the port a pins with alte rnate function are shown in table 10-3 . ? port a, bit 0 ? xtal1/clki/pcint8 ? xtal1: chip clock oscillator pin 1. used fo r all chip clock sour ces except internal calibratable rc oscillator. when used as a clock pin, the pin can not be used as an i/o pin. when using internal calibratabl e rc oscillator as a chip cloc k source, pa0 serves as an ordinary i/o pin. ? clki: clock input from an external clock source, see ?external clock? on page 25 . ? pcint8: pin change interrupt source 8. the pa0 pin can serve as an external interrupt source for pin change interrupt 1. ? port a, bit 1 ? xtal2/pcint9 ? xtal2: chip clock oscillator pin 2. used as clock pin for all chip clock sources except internal calibratable rc oscillator and ex ternal clock. when used as a clock pin, the pin can not be used as an i/o pin. when using internal calibratable rc oscillator or external clock as a chip clock sources, pa1 serves as an ordinary i/o pin. ? pcint9: pin change interrupt source 9. the pa1 pin can serve as an external interrupt source for pin change interrupt 1. ? port a, bit 2 ? reset /dw/pcint10 ? reset : external reset input is active low and enabled by unprogramming (?1?) the rstdisbl fuse. pullup is activated and output driver and digital input are deactivated when the pin is used as the reset pin. ? dw: when the debugwire enable (dwen) fuse is programmed and lock bits are unprogrammed, the debugwire system within th e target device is activated. the reset port pin is configured as a wire-and (open-dr ain) bi-directional i/o pin with pull-up enabled and becomes the communication gateway between target and emulator. ? pcint10: pin change interrupt source 10. the pa2 pin can serve as an external interrupt source for pin change interrupt 1. table 10-3. port a pins alternate functions port pin alternate function pa0 xtal1: crystal oscillator input clki: external clock input pcint8: pin change interrupt 1, source 8 pa1 xtal2: crystal oscillator output pcint9: pin change interrupt 1, source 9 pa2 reset : reset pin dw: debugwire i/o pcint10:pin change interrupt 1, source 10
62 8246a?avr?11/09 attiny2313a/4313 table 10-4 relates the alternate functions of port a to the overriding signals shown in figure 10- 5 on page 59 . notes: 1. rstdisbl is 1 when the fuse is ?0? (programmed). 2. debugwire is enabled when dwen fuse is programmed and lock bits are unprogrammed. 3. ext_osc = crystal oscillator or low frequency crystal oscillator is selected as system clock. 4. ext_clock = external closk is selected as system clock. 10.2.2 alternate functions of port b the port b pins with alte rnate function are shown in table 10-5 . table 10-4. overriding signals for alternate functions in pa2..pa0 signal name pa2/reset/dw/pcint10 pa1/x tal2/pcint9 pa0/xtal1/pcint8 puoe rstdisbl (1) + debugwire_enable (2) ext_osc (3) ext_clock (4) + ext_osc (3) puov 1 0 0 ddoe rstdisbl (1) + debugwire_enable (2) ext_osc (3) ext_clock (4) + ext_osc (3) ddov debugwire_enable (2) ? debugwire transmit 00 pvoe rstdisbl (1) + debugwire_enable (2) ext_osc (3) ext_clock (4) + ext_osc (3) pvov 0 0 0 ptoe 0 0 0 dieoe rstdisbl (1) + debugwire_enable (2) + pcint10 ? pcie1 ext_osc (3) + pcint9 ? pcie1 ext_clock (4) + ext_osc (3) + (pcint8 ? pcie1) dieov debugwire_enable (2) + (rstdisbl (1) ? pcint10 ? pcie1) ext_osc (3) + pcint9 ? pcie1 (ext_clock (4) ? pwr_down ) + (ext_cloc k (4) ? ext_osc (3) ? pcint8 ? pcie1) di dw/pcint10 input pcint 9 input clki/pcint8 input aio xtal2 xtal1 table 10-5. port b pins alternate functions port pin alternate function pb0 ain0: analog comparator, positive input pcint0:pin change interrupt 0, source 0 pb1 ain1: analog comparator, negative input pcint1: pin change interrupt 0, source 1 pb2 oc0a:: timer/counter0 compare match aoutput pcint2: pin change interrupt 0, source 2 pb3 oc1a: timer/counter1 compare match a output pcint3: pin change interrupt 0, source 3
63 8246a?avr?11/09 attiny2313a/4313 ? port b, bit 0 ? ain0/pcint0 ? ain0: analog comparator positive input. configure the port pin as input with the internal pull- up switched off to avoid the digital port functi on from interfering with the function of the analog comparator. ? pcint0: pin change interrupt source 0. t he pb0 pin can serve as an external interrupt source for pin change interrupt 0. ? port b, bit 1 ? ain1/pcint1 ? ain1: analog comparator negative input . configure the port pin as input with the internal pull-up switched off to avoid the digital port f unction from interfering with the function of the analog comparator. ? pcint1: pin change interrupt source 1. t he pb1 pin can serve as an external interrupt source for pin change interrupt 0. ? port b, bit 2 ? oc0a/pcint2 ? oc0a: output compare match a output. the pb2 pin can serve as an external output for the timer/counter0 output compare a. the pin has to be configured as an output (ddb2 set (one)) to serve this function. the oc0a pin is also the output pin for the pwm mode timer function. ? pcint2: pin change interrupt source 2. t he pb2 pin can serve as an external interrupt source for pin change interrupt 0. ? port b, bit 3 ? oc1a/pcint3 ? oc1a: output compare match a output: the pb3 pin can serve as an external output for the timer/counter1 output compare a. the pin has to be configured as an output (ddb3 set (one)) to serve this function. the oc1a pin is also the output pin for the pwm mode timer function. ? pcint3: pin change interrupt source 3: t he pb3 pin can serve as an external interrupt source for pin change interrupt 0. ? port b, bit 4 ? oc1b/pcint4 ? oc1b: output compare match b output: the pb4 pin can serve as an external output for the timer/counter1 output compare b. the pin has to be configured as an output (ddb4 set pb4 oc1b: timer/counter1 compare match b output pcint4: pin change interrupt 0, source 4 pb5 di: usi data input (three wire mode) sda: usi data input (two wire mode) pcint5: pin change interrupt 0, source 5 pb6 do: usi data output (three wire mode) pcint6: pin change interrupt 0, source 6 pb7 usck: usi clock (three wire mode) scl : usi clock (two wire mode) pcint7: pin change interrupt 0, source 7 table 10-5. port b pins alternate functions port pin alternate function
64 8246a?avr?11/09 attiny2313a/4313 (one)) to serve this function. the oc1b pin is also the output pin for the pwm mode timer function. ? pcint4: pin change interrupt source 4. t he pb4 pin can serve as an external interrupt source for pin change interrupt 0. ? port b, bit 5 ? di/sda/pcint5 ? di: three-wire mode universal serial interface data input. three-wire mode does not override normal port functions, so pin must be configured as an input. sda: two-wire mode serial interface data. ? pcint5: pin change interrupt source 5. t he pb5 pin can serve as an external interrupt source for pin change interrupt 0. ? port b, bit 6 ? do/pcint6 ? do: three-wire mode universal serial interface data output. three-wire mode data output overrides portb6 value and it is driven to the port when data direction bit ddb6 is set (one). however the portb6 bit still cont rols the pull-up enabling pull-up, if direction is input and portb6 is set (one). ? pcint6: pin change interrupt source 6. t he pb6 pin can serve as an external interrupt source for pin change interrupt 0. ? port b, bit 7 ? usck/scl/pcint7 ? usck: three-wire mode univer sal serial interface clock. ? scl: two-wire mode serial clock for usi two-wire mode. ? pcint7: pin change interrupt source 7. th e pb7 pin can serve as an external interrupt source for pin change interrupt 0. table 10-6 and table 10-7 relate the alternate functions of port b to the overriding signals shown in figure 10-5 on page 59 . spi mstr input and spi sl ave output constitute the miso signal, while mosi is divided in to spi mstr output and spi slave input.
65 8246a?avr?11/09 attiny2313a/4313 table 10-6. overriding signals for alternate functions in pb7..pb4 signal name pb7/usck/ scl/pcint7 pb6/do/pcint6 pb5/sda/ di/pcint5 pb4/oc1b/ pcint4 puoe usi_two_wire 0 0 0 puov 0 0 0 0 ddoe usi_two_wire 0 usi_two_wire 0 ddov (usi_scl_hold+ portb7 )?ddb7 0 (sda + portb5 )? ddb5 0 pvoe usi_two_wire ? ddb7 usi_three_wire usi_two_wire ? ddb5 oc1b_pvoe pvov 0 do 0 0oc1b_pvov ptoe usi_ptoe 0 0 0 dieoe (pcint7?pcie) +usisie (pcint6?pcie) (pcint5?pcie) + usisie (pcint4?pcie) dieov 1 1 1 1 di pcint7 input usck input scl input pcint6 input pcint5 input sda input di input pcint4 input aio ? ? ? ? table 10-7. overriding signals for alternate functions in pb3..pb0 signal name pb3/oc1a/ pcint3 pb2/oc0a/ pcint2 pb1/ain1/ pcint1 pb0/ain0/ pcint0 puoe 0 0 0 0 puov 0 0 0 0 ddoe 0 0 0 0 ddov 0 0 0 0 pvoe oc1a_pvoe oc0a_pvoe 0 0 pvov oc1a_pvov oc0a_pvov 0 0 ptoe 0 0 0 0 dieoe (pcint3 ? pcie) (pcint2 ? pcie ) (pcint1 ? pcie) (pcint0 ? pcie) dieov 1 1 1 1 di pcint7 input pcint6 input pcint5 input pcint4 input aio ? ? ain1 ain0
66 8246a?avr?11/09 attiny2313a/4313 10.2.3 alternate functions of port d the port d pins with alternate functions are shown in table 10-8 .. ? port d, bit 0 ? rxd/pcint11 ? rxd: uart data receiver. ? pcint11: pin change interrupt source 11. the pd0 pin can serve as an external interrupt source for pin change interrupt 2. ? port d, bit 1 ? txd/pcint12 ? txd: uart data transmitter. ? pcint12: pin change interrupt source 12. the pd1 pin can serve as an external interrupt source for pin change interrupt 2. ? port d, bit 2 ? int0/xck/ckout/pcint13 ? int0: external interrupt source 0. the pd2 pin can serve as en external interrupt source to the mcu. ? xck: usart transfer clock used on ly by synchronous transfer mode. ? ckout: system clock output. ? pcint13: pin change interrupt source 13. the pd2 pin can serve as an external interrupt source for pin change interrupt 2. ? port d, bit 3 ? int1/pcint14 ? int1: external interrupt source 1. the pd3 pin can serve as an external interrupt source to the mcu. ? pcint14: pin change interrupt source 14. the pd3 pin can serve as an external interrupt source for pin change interrupt 2. table 10-8. port d pins alternate functions port pin alternate function pd0 rxd: uart data receiver pcint11:pin change interrupt 2, source 11 pd1 txd: uart data transmitter pcint12:pin change interrupt 2, source 12 pd2 int0: external interrupt 0 input xck: usart transfer clock ckout: system clock output pcint13:pin change interrupt 2, source 13 pd3 int1: external interrupt 1 input pcint14:pin change interrupt 2, source 14 pd4 t0: timer/counter0 clock source pcint15:pin change interrupt 2, source 15 pd5 oc0b: timer/counter0 compare match b output t1: timer/counter1 clock source pcint16:pin change interrupt 2, source 16 pd6 icpi: timer/counter1 input capture pin pcint17:pin change interrupt 2, source 17
67 8246a?avr?11/09 attiny2313a/4313 ? port d, bit 4 ? t0/pcint15 ? t0: timer/counter0 external counter clock input is enabled by setting (one) the bits cs02 and cs01 in the timer/counter0 control register (tccr0). ? pcint15: pin change interrupt source 15. the pd4 pin can serve as an external interrupt source for pin change interrupt 2. ? port d, bit 5 ? oc0b/t1/pcint16 ? oc0b: output compare match b output: the pd5 pin can serve as an external output for the timer/counter0 output compare b. the pin has to be co nfigured as an output (ddd5 set (one)) to serve this function. the oc0b pin is also the output pin for the pwm mode timer function. ? t1: timer/counter1 external counter clock input is enabled by setting (one) the bits cs02 and cs01 in the timer/counter1 control register (tccr1). ? pcint16: pin change interrupt source 16. the pd5 pin can serve as an external interrupt source for pin change interrupt 2. ? port d, bit 6 ? icpi/pcint17 ? icpi: timer/counter1 input capture pin. the pd6 pin can act as an input capture pin for timer/counter1. ? pcint17: pin change interrupt source 17. the pd6 pin can serve as an external interrupt source for pin change interrupt 2. table 10-9 and table 10-10 relates the alternate functions of port d to the overriding signals shown in figure 10-5 on page 59 . table 10-9. overriding signals for alternate functions pd6..pd4 signal name pd6/icpi/pcint17 pd5/oc1b /t1/pcint16 pd4/t0/pcint15 puoe 0 0 0 puov 0 0 0 ddoe 0 0 0 ddov 0 0 0 pvoe 0 oc1b_pvoe 0 pvov 0 oc1b_pvov 0 ptoe 0 0 0 dieoe icpi enable + pcint17 t1 enable + pcint16 t0 enable + pcint15 dieov pcint17 pcint16 pcint15 di icpi input/pcint17 t1 inpu t/pcint16 t0 input/pcint15 aio ? ? ain1
68 8246a?avr?11/09 attiny2313a/4313 10.3 register description 10.3.1 mcucr ? mcu control register ? bit 7 ? pud: pull-up disable when this bit is written to one, the pull-ups in the i/o ports are disabled even if the ddxn and portxn registers are configured to enable the pull-ups ({ddxn, portxn} = 0b01). see ?con- figuring the pin? on page 55 for more details about this feature. 10.3.2 porta ? port a data register 10.3.3 ddra ? port a da ta direction register table 10-10. overriding signals for alternate functions in pd3..pd0 signal name pd3/int1/ pcint14 pd2/int0/xck/ckout/ pcint13 pd1/txd/ pcint12 pd0/rxd/pcint11 puoe 0 0 txd_oe rxd_oe puov 0 0 0 portd0 ? pud ddoe 0 0 txd_oe rxd_en ddov 0 0 1 0 pvoe 0 xcko_pvoe txd_oe 0 pvov 0 xcko_pvov txd_pvov 0 ptoe 0 0 0 0 dieoe int1 enable + pcint14 int0 enable/ xck input enable/pcint13 pcint12 pcint11 dieov pcint14 pcint13 pcint12 pcint11 di int1 input/ pcint14 int0 input/xck input/ pcint13 pcint12 rxd input/pcint11 aio ? ? ? ? bit 7 6 5 4 3 2 1 0 0x35 (0x55) pud sm1 se sm0 isc11 isc10 isc01 isc00 mcucr read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 bit 76543210 0x1b (0x3b) ?????porta2porta1porta0 porta read/write rrrrrr/wr/wr/w initial value00000000 bit 76543210 0x1a (0x3a) ????? dda2 dda1 dda0 ddra read/write rrrrrr/wr/wr/w initial value00000000
69 8246a?avr?11/09 attiny2313a/4313 10.3.4 pina ? port a input pins address 10.3.5 portb ? port b data register 10.3.6 ddrb ? port b da ta direction register 10.3.7 pinb ? port b input pins address 10.3.8 portd ? port d data register 10.3.9 ddrd ? port d da ta direction register 10.3.10 pind ? port d input pins address bit 76543210 0x19 (0x39) ????? pina2 pina1 pina0 pina read/writerrrrrr/wr/wr/w initial value n/a n/a n/a n/a n/a n/a n/a n/a bit 76543210 0x18 (0x38) portb7 portb6 portb5 portb4 portb3 portb2 portb1 portb0 portb read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 bit 76543210 0x17 (0x37) ddb7 ddb6 ddb5 ddb4 ddb3 ddb2 ddb1 ddb0 ddrb read/writer/wr/wr/wr/wr/wr/wr/wr/w initial value00000000 bit 76543210 0x16 (0x36) pinb7 pinb6 pinb5 pinb4 pi nb3 pinb2 pinb1 pinb0 pinb read/writer/wr/wr/wr/wr/wr/wr/wr/w initial value n/a n/a n/a n/a n/a n/a n/a n/a bit 76543210 0x12 (0x32) ? portd6 portd5 portd4 portd3 portd2 portd1 portd0 portd read/write r r/wr/wr/wr/wr/wr/wr/w initial value00000000 bit 76543210 0x11 (0x31) ? ddd6 ddd5 ddd4 ddd3 ddd2 ddd1 ddd0 ddrd read/write r r/wr/wr/wr/wr/wr/wr/w initial value00000000 bit 76543210 0x10 (0x30) ? pind6 pind5 pind4 pind3 pind2 pind1 pind0 pind read/write r r/wr/wr/wr/wr/wr/wr/w initial value n/a n/a n/a n/a n/a n/a n/a n/a
70 8246a?avr?11/09 attiny2313a/4313 11. 8-bit timer/counter0 with pwm 11.1 features ? two independent output compare units ? double buffered output compare registers ? clear timer on compare match (auto reload) ? glitch free, phase correct pulse width modulator (pwm) ? variable pwm period ? frequency generator ? three independent interrupt sources (tov0, ocf0a, and ocf0b) 11.2 overview timer/counter0 is a general purpose 8-bit time r/counter module, with two independent output compare units, and with pwm support. it allows accurate program execution timing (event man- agement) and wave generation. a simplified block diagram of the 8-bit timer/counter is shown in figure 11-1 . for the actual placement of i/o pins, refer to ?pinout attiny2313a/4313? on page 2 . cpu accessible i/o regis- ters, including i/o bits and i/o pins , are shown in bold. the device- specific i/o register and bit locations are listed in the ?register description? on page 81 . figure 11-1. 8-bit timer/counter block diagram 11.2.1 registers the timer/counter (tcnt0) and output compare registers (ocr0a and ocr0b) are 8-bit registers. interrupt request (abbreviated to int.req . in the figure) signals are all visible in the timer interrupt flag register (tifr). all interrup ts are individually masked with the timer inter- rupt mask register (timsk). tifr and timsk are not shown in the figure. the timer/counter can be clocked internally, via th e prescaler, or by an external clock source on the t0 pin. the clock select logic block contro ls which clock source and edge the timer/counter clock select timer/counter data bus ocrna ocrnb = = tcntn waveform generation waveform generation ocfna ocfnb = fixed top value control logic = 0 top bottom count clear direction tovn (int.req.) ocfna (int.req.) ocfnb (int.req.) tccrna tccrnb tn edge detector ( from prescaler ) clk tn
71 8246a?avr?11/09 attiny2313a/4313 uses to increment (or decrement) its value. th e timer/counter is inactive when no clock source is selected. the output from th e clock select logic is referred to as the timer clock (clk t0 ). the double buffered output compare registers (ocr0a and ocr0b) is compared with the timer/counter value at all times. the result of the compare can be used by the waveform gen- erator to generate a pwm or variable frequency output on the output compare pins (oc0a and oc0b). see ?output compare unit? on page 72. for details. the comp are match event will also set the compare flag (ocf0a or ocf0b) which can be used to generate an output compare interrupt request. 11.2.2 definitions many register and bit references in this section are written in general form. a lower case ?n? replaces the timer/counter number, in this case 0. a lower case ?x? replaces the output com- pare unit, in this case compare unit a or comp are unit b. however, when using the register or bit defines in a program, the precise form must be used, i.e., tcnt0 for accessing timer/counter0 counter value and so on. the definitions in table 11-1 are also used extensively throughout the document. 11.3 clock sources the timer/counter can be clocked by an internal or an external clock source. the clock source is selected by the clock select logic which is controlled by the clock select (cs02:0) bits located in the timer/counter control register (tccr0b). for details on clock sources and pres- caler, see ?timer/counter0 and timer/counter1 prescalers? on page 117 . 11.4 counter unit the main part of the 8-bit timer/counter is the programmable bi-directional counter unit. figure 11-2 shows a block diagram of the counter and its surroundings. figure 11-2. counter unit block diagram table 11-1. definitions constant description bottom the counter reaches bo ttom when it becomes 0x00 max the counter reaches its maximum wh en it becomes 0xff (decimal 255) top the counter reaches the top when it becomes equal to the highest value in the count sequence. the top value can be assigned to be the fixed value 0xff (max) or the value stored in the ocr0a register. the assignment depends on the mode of operation data b u s tcntn control logic count tovn (int.req.) clock select top tn edge detector ( from prescaler ) clk tn bottom direction clear
72 8246a?avr?11/09 attiny2313a/4313 signal description (internal signals): count increment or decrement tcnt0 by 1. direction select between increment and decrement. clear clear tcnt0 (set all bits to zero). clk t n timer/counter clock, referred to as clk t0 in the following. top signalize that tcnt0 has reached maximum value. bottom signalize that tcnt0 has re ached minimum value (zero). depending of the mode of operation used, the counter is cleared, incremented, or decremented at each timer clock (clk t0 ). clk t0 can be generated from an external or internal clock source, selected by the clock select bits (cs02:0). w hen no clock source is selected (cs02:0 = 0) the timer is stopped. however, the tcnt0 value can be accessed by the cpu, regardless of whether clk t0 is present or not. a cpu write overrides (has priority over) all counter clear or count operations. the counting sequence is determined by the se tting of the wgm01 and wgm00 bits located in the timer/counter control regist er (tccr0a) and the wgm02 bit located in the timer/counter control register b (tccr0b). there are clos e connections between how the counter behaves (counts) and how waveforms are generated on the output compare output oc0a. for more details about advanced counting s equences and waveform generation, see ?modes of opera- tion? on page 96 . the timer/counter overflow flag (tov0) is set a ccording to the mode of operation selected by the wgm01:0 bits. tov0 can be us ed for generating a cpu interrupt. 11.5 output compare unit the 8-bit comparator continuously compares tcnt0 with the output compare registers (ocr0a and ocr0b). whenever tcnt0 equals ocr0 a or ocr0b, the comparator signals a match. a match will set the output compare flag (ocf0a or ocf0 b) at the next timer clock cycle. if the corresponding interrupt is enabled, the output compare flag generates an output compare interrupt. the output compare flag is aut omatically cleared when the interrupt is exe- cuted. alternatively, the flag can be cleared by software by writing a logical one to its i/o bit location. the waveform generator uses the matc h signal to generate an output according to operating mode set by the wgm02:0 bits and compare output mode (com0x1:0) bits. the max and bottom signals are used by the waveform generator for handling the special cases of the extreme values in some modes of operation (see ?modes of operation? on page 96 ). figure 11-3 shows a block diagram of the output compare unit.
73 8246a?avr?11/09 attiny2313a/4313 figure 11-3. output compare unit, block diagram the ocr0x registers are double buffered when using any of the pulse width modulation (pwm) modes. for the normal and clear timer on compare (ctc) modes of operation, the dou- ble buffering is disabled. the double buffering synchronizes the update of the ocr0x compare registers to either top or bottom of the counting sequence. the synchronization prevents the occurrence of odd-length, non-symmetrical pwm pulses, thereby making the output glitch-free. the ocr0x register access may seem complex, but this is not case. when the double buffering is enabled, the cpu has access to the ocr0x buffer register, and if double buffering is dis- abled the cpu will access the ocr0x directly. 11.5.1 force output compare in non-pwm waveform generation modes, the matc h output of the comparator can be forced by writing a one to the force outp ut compare (foc0x) bit. forcin g compare match will not set the ocf0x flag or reload/clear the timer, but the oc0x pin will be updated as if a real compare match had occurred (the com0x1:0 bits settings de fine whether the oc0x pin is set, cleared or toggled). 11.5.2 compare match blocking by tcnt0 write all cpu write operations to the tcnt0 register will block any compare ma tch that occur in the next timer clock cycle, even when the timer is stopped. this feat ure allows ocr0x to be initial- ized to the same value as tcnt0 without trigge ring an interrupt when the timer/counter clock is enabled. 11.5.3 using the output compare unit since writing tcnt0 in any mo de of operation will block all compare matches for one timer clock cycle, there are risks involved when ch anging tcnt0 when using the output compare unit, independently of whether the timer/counter is running or not. if the value written to tcnt0 equals the ocr0x value, the compare match will be missed, resulting in incorrect waveform ocfn x (int.req.) = (8-bit comparator ) ocrnx ocnx data bus tcntn wgmn1:0 waveform generator top focn comnx1:0 bottom
74 8246a?avr?11/09 attiny2313a/4313 generation. similarly, do not write the tcnt0 value equal to bottom when the counter is down-counting. the setup of the oc0x should be performed before setting the data direction register for the port pin to output. the easiest way of setting the oc0x value is to use the force output com- pare (foc0x) strobe bits in normal mode. the oc0x registers keep their values even when changing between waveform generation modes. be aware that the com0x1:0 bits are not doubl e buffered together with the compare value. changing the com0x1:0 bits will take effect immediately. 11.6 compare match output unit the compare output mode (com0x1:0) bits ha ve two functions. the waveform generator uses the com0x1:0 bits for defining the output co mpare (oc0x) state at the next compare match. also, the com0x1:0 bits contro l the oc0x pin output source. figure 11-4 shows a simplified schematic of the logic affected by the com0x1:0 bit setting. the i/o registers, i/o bits, and i/o pins in the figure are shown in bold. only the parts of the general i/o port control registers (ddr and port) that are affected by the com0x1:0 bits are shown. when referring to the oc0x state, the reference is for the internal oc0x register, not the oc0x pin. if a system reset occur, the oc0x regist er is reset to ?0?. figure 11-4. compare match output unit, schematic the general i/o port function is overridden by the output compare (oc0x) from the waveform generator if either of the com0x1:0 bits are se t. however, the oc0x pin direction (input or out- put) is still controlled by the da ta direction register (ddr) for th e port pin. the data direction register bit for the oc0x pin (ddr_oc0x) must be set as output before th e oc0x value is visi- ble on the pin. the port over ride function is independent of the waveform generation mode. the design of the output compare pin logic allows initialization of the oc 0x state before the out- put is enabled. note that some com0x1:0 bi t settings are reserved for certain modes of operation. see ?register description? on page 81. port ddr dq dq ocn pin ocnx dq waveform generator comnx1 comnx0 0 1 data bus focn clk i/o
75 8246a?avr?11/09 attiny2313a/4313 11.6.1 compare output mode and waveform generation the waveform generator uses the com0x1:0 bits differently in normal, ctc, and pwm modes. for all modes, setting the com0x1:0 = 0 tell s the waveform generator that no action on the oc0x register is to be perfo rmed on the next compare match. for compare output actions in the non-pwm modes refer to figure 11-3 on page 73 . for fast pwm mode, refer to table 10-6 on page 65 , and for phase correct pwm refer to table 10-7 on page 65 . a change of the com0x1:0 bits state will have effe ct at the first compare match after the bits are written. for non-pwm modes, the action can be fo rced to have immediate effect by using the foc0x strobe bits. 11.7 modes of operation the mode of operation, i.e., t he behavior of the timer/counter and the output compare pins, is defined by the combination of the waveform generation mode (wgm02:0) and compare output mode (com0x1:0) bits. the compare output mode bits do not affect the counting sequence, while the waveform generation mode bits do. the com0x1:0 bits control whether the pwm out- put generated should be inverted or not (inverted or non-inverted pwm). for non-pwm modes the com0x1:0 bits control whether the output should be set, cleared, or toggled at a compare match ( see ?compare match output unit? on page 74. ). for detailed timing information refer to figure 11-8 , figure 11-9 , figure 11-10 and figure 11-11 in ?timer/counter timing diagrams? on page 79 . 11.7.1 normal mode the simplest mode of operation is the normal mode (wgm02:0 = 0). in this mode the counting direction is always up (incre menting), and no counter clear is performed. the counter simply overruns when it passes its maxi mum 8-bit value (top = 0xff) and then restarts from the bot- tom (0x00). in normal o peration the timer/counter overflow flag (tov0) will be set in the same timer clock cycle as the tcnt0 becomes zero. the tov0 flag in this case behaves like a ninth bit, except that it is only set, not cleared. ho wever, combined with the timer overflow interrupt that automatically clears the tov0 flag, the timer resolution can be increased by software. there are no special cases to consider in the normal mode, a new counter value can be written anytime. the output compare unit can be used to generate interrupts at some given time. using the out- put compare to generate waveforms in normal mode is not recommended, since this will occupy too much of the cpu time. 11.7.2 clear timer on compare match (ctc) mode in clear timer on compare or ctc mode (wgm 02:0 = 2), the ocr0a register is used to manipulate the counter resolution . in ctc mode the counter is cleared to zero when the counter value (tcnt0) matches the ocr0a. the ocr0a de fines the top value fo r the counter, hence also its resolution. this mode allows greater control of the compare match output frequency. it also simplifies the operation of counting external events. the timing diagram for the ctc mode is shown in figure 11-5 . the counter value (tcnt0) increases until a compare match occurs between tcnt0 and ocr0a, and then counter (tcnt0) is cleared.
76 8246a?avr?11/09 attiny2313a/4313 figure 11-5. ctc mode, timing diagram an interrupt can be generated each time the c ounter value reaches the top value by using the ocf0a flag. if the interrupt is enabled, the interrupt handler routine can be used for updating the top value. however, changing top to a va lue close to bottom when the counter is run- ning with none or a low prescaler value must be done with care since the ctc mode does not have the double buffering feature. if the new value written to ocr0a is lower than the current value of tcnt0, the counter will miss the compar e match. the counter will then have to count to its maximum value (0xff) and wrap around starting at 0x00 before the compare match can occur. for generating a waveform output in ctc mode, t he oc0a output can be set to toggle its logical level on each compare match by setting the compare output mode bits to toggle mode (com0a1:0 = 1). the oc0a value will not be visible on the port pin unless the data direction for the pin is set to output. the waveform ge nerated will have a ma ximum frequency of f oc0 = f clk_i/o /2 when ocr0a is set to zero (0x00). the waveform frequency is defined by the following equation: the n variable represents the prescale factor (1, 8, 64, 256, or 1024). as for the normal mode of operat ion, the tov0 flag is set in the same timer clock cycle that the counter counts fr om max to 0x00. 11.7.3 fast pwm mode the fast pulse width modulation or fast pwm mode (wgm02:0 = 3 or 7) provides a high fre- quency pwm waveform generation option. the fa st pwm differs from the other pwm option by its single-slope operation. the c ounter counts from bottom to top then restarts from bot- tom. top is defined as 0xff when wgm2:0 = 3, and ocr0a when wgm2:0 = 7. in non- inverting compare output mode, the output compare (oc0x) is cleared on the compare match between tcnt0 and ocr0x, and set at bottom. in inverting compare output mode, the out- put is set on compare match and cleared at bottom. due to the single-slope operation, the operating frequency of the fast pwm mode can be twice as high as the phase correct pwm mode that use dual-slope operation. this high frequency makes the fast pwm mode well suited for power regulation, rectification, and dac app lications. high frequency a llows physically small sized external components (coils, capacitors), and therefore reduces total system cost. in fast pwm mode, the counter is incremented until the counter value matches the top value. the counter is then cleared at the following timer clock cycle. the timing diagram for the fast t cntn o cn ( toggle) ocnx interrupt flag set 1 4 p eriod 2 3 (comnx1:0 = 1) f ocnx f clk_i/o 2 n 1 ocrnx + () ?? ------------------------------------------------- - =
77 8246a?avr?11/09 attiny2313a/4313 pwm mode is shown in figure 11-4 . the tcnt0 value is in the timing diagram shown as a his- togram for illustrating the single-slope operation. the diagram includes non-inverted and inverted pwm outputs. the small horizontal line marks on the tcnt0 slopes represent com- pare matches between ocr0x and tcnt0. figure 11-6. fast pwm mode, timing diagram the timer/counter overflow flag (tov0) is set each time the counter re aches top. if the inter- rupt is enabled, the interrupt handler routi ne can be used for updating the compare value. in fast pwm mode, the compare unit allows generation of pwm waveforms on the oc0x pins. setting the com0x1:0 bits to two will produce a non-inverted pwm and an inverted pwm output can be generated by setting the com0x1:0 to th ree: setting the com0a1:0 bits to one allows the ac0a pin to toggle on compare matches if t he wgm02 bit is set. this option is not available for the oc0b pin (see table 10-6 on page 65 ). the actual oc0x value will only be visible on the port pin if the data direction fo r the port pin is set as output. the pwm waveform is generated by setting (or clearing) the oc0x register at the compare match between ocr0x and tcnt0, and clearing (or setting) the oc0x r egister at the timer clock c ycle the counter is cleared (changes from top to bottom). the pwm frequency for the output can be calculated by the following equation: the n variable represents the prescale factor (1, 8, 64, 256, or 1024). the extreme values for the ocr0a register represents special cases when generating a pwm waveform output in the fast pwm mode. if t he ocr0a is set equal to bottom, the output will be a narrow spike for each max+1 timer clock cycle. setting the ocr0a equal to max will result in a constantly high or low output (depending on the polarity of the out put set by the com0a1:0 bits.) a frequency (with 50% duty cycle) waveform out put in fast pwm mode can be achieved by set- ting oc0x to toggle its logical level on each compare match (com0x1:0 = 1). the waveform generated will have a maximum frequency of f oc0 = f clk_i/o /2 when ocr0a is set to zero. this t cntn ocrnx update and tovn interrupt flag set 1 p eriod 2 3 o cn o cn (comnx1:0 = 2) (comnx1:0 = 3) ocrnx interrupt flag se t 4 5 6 7 f ocnxpwm f clk_i/o n 256 ? ------------------ =
78 8246a?avr?11/09 attiny2313a/4313 feature is similar to the oc0a toggle in ctc mo de, except the double buff er feature of the out- put compare unit is enabled in the fast pwm mode. 11.7.4 phase correct pwm mode the phase correct pwm mode (wgm02:0 = 1 or 5) provides a high resolution phase correct pwm waveform generation option. the phase correct pwm mode is based on a dual-slope operation. the counter counts repeatedly from bottom to top and then from top to bot- tom. top is defined as 0xff when wgm2:0 = 1, and ocr0a when wgm2:0 = 5. in non- inverting compare output mode, the output compare (oc0x) is cleared on the compare match between tcnt0 and ocr0x while upcounting, and set on the compare match while down- counting. in inverting output compare mode, the operation is inverted. the dual-slope operation has lower maximum operation frequency than single slope operation. however, due to the sym- metric feature of the dual-slope pwm modes, t hese modes are preferred for motor control applications. in phase correct pwm mode the counter is incremented until the counter value matches top. when the counter reaches top, it changes the count direction. the tcnt0 value will be equal to top for one timer clock cycle. the timing diagram for the phase correct pwm mode is shown on figure 11-7 . the tcnt0 value is in the timing diagram shown as a histogram for illustrating the dual-slope operation. the diagram includes non-inverted and inverted pwm outputs. the small horizontal line marks on the tcnt0 slop es represent compare matches between ocr0x and tcnt0. figure 11-7. phase correct pwm mode, timing diagram the timer/counter overflow flag (tov0) is set each time the counter reaches bottom. the interrupt flag can be used to g enerate an interrupt each time the counter reaches the bottom value. in phase correct pwm mode, the compare unit allows generation of pwm waveforms on the oc0x pins. setting the com0x1:0 bits to two will produce a non-inverted pwm. an inverted pwm output can be generated by setting the com0x1:0 to three: setting the com0a0 bits to tovn interrupt flag set ocnx interrupt flag set 1 2 3 tcntn period ocn ocn (comnx1:0 = 2) (comnx1:0 = 3) ocrnx update
79 8246a?avr?11/09 attiny2313a/4313 one allows the oc0a pin to toggle on compare ma tches if the wgm02 bit is set. this option is not available for the oc0b pin (see table 10-7 on page 65 ). the actual oc0x value will only be visible on the port pin if the data direction for the port pin is set as output. the pwm waveform is generated by clearing (or setting) the oc0x register at the compare match between ocr0x and tcnt0 when the counter increments, and setti ng (or clearing) the oc0x register at com- pare match between ocr0x and tcnt0 when the counter decrements. the pwm frequency for the output when using phase correct pwm c an be calculated by the following equation: the n variable represents the prescale factor (1, 8, 64, 256, or 1024). the extreme values for the ocr0a register represent special cases when generating a pwm waveform output in the phase correct pwm mo de. if the ocr0a is set equal to bottom, the output will be continuously low an d if set equal to max the output will be continuously high for non-inverted pwm mode. for in verted pwm the output will have the opposite logic values. at the very start of period 2 in figure 11-7 on page 78 ocn has a transition from high to low even though there is no compare match. the poin t of this transition is to guarantee symmetry around bottom. there are two cases that gi ve a transition without compare match. ? ocr0a changes its valu e from max, like in figure 11-7 on page 78 . when the ocr0a value is max the ocn pin value is the same as the result of a down-counting compare match. to ensure symmetry around bottom the ocn value at max must correspond to the result of an up-counting compare match. ? the timer starts counting from a value higher than the one in ocr0a, and for that reason misses the compare match and hence the ocn change that would have happened on the way up. 11.8 timer/counter timing diagrams the timer/counter is a synchronous design and the timer clock (clk t0 ) is therefore shown as a clock enable signal in the following figures. the figures include information on when interrupt flags are set. figure 11-8 contains timing data for basic timer/counter operation. the figure shows the count sequence close to the max value in all modes other than phase correct pwm mode. figure 11-8. timer/counter timing diagram, no prescaling figure 11-9 on page 80 shows the same timing data, but with the prescaler enabled. f ocnxpcpwm f clk_i/o n 510 ? ------------------ = clk tn (clk i/o /1) tovn clk i/o tcntn max - 1 max bottom bottom + 1
80 8246a?avr?11/09 attiny2313a/4313 figure 11-9. timer/counter timing diagram, with prescaler (f clk_i/o /8) figure 11-10 shows the setting of ocf0b in all mo des and ocf0a in all modes except ctc mode and pwm mode, where ocr0a is top. figure 11-10. timer/counter timing diagram, setting of ocf0x, with prescaler (f clk_i/o /8) figure 11-11 shows the setting of ocf0a and the clearing of tcnt0 in ctc mode and fast pwm mode where ocr0a is top. figure 11-11. timer/counter timing diagram, clear timer on compare match mode, with pres- caler (f clk_i/o /8) tovn tcntn max - 1 max bottom bottom + 1 clk i/o clk tn (clk i/o /8) ocfnx ocrnx tcntn ocrnx value ocrnx - 1 ocrnx ocrnx + 1 ocrnx + 2 clk i/o clk tn (clk i/o /8) ocfnx ocrnx tcntn (ctc) top top - 1 top bottom bottom + 1 clk i/o clk tn (clk i/o /8)
81 8246a?avr?11/09 attiny2313a/4313 11.9 register description 11.9.1 tccr0a ? timer/coun ter control register a ? bits 7:6 ? com0a1:0: compare match output a mode these bits control the output compare pin (oc0a) behavior. if one or both of the com0a1:0 bits are set, the oc0a output overrides the normal po rt functionality of the i/o pin it is connected to. however, note that the data direction r egister (ddr) bit corresponding to the oc0a pin must be set in order to enable the output driver. when oc0a is connected to the pin, the function of the com0a1:0 bits depends on the wgm02:0 bit setting. table 11-2 shows the com0a1:0 bit func tionality when the wgm02:0 bits are set to a normal or ctc mode (non-pwm). table 11-3 shows the com0a1:0 bit functionality when the wgm01:0 bits are set to fast pwm mode. note: 1. a special case occurs when ocr0a equals top and com0a1 is set. in this case, the com- pare match is ignored, but the se t or clear is done at top. see ?fast pwm mode? on page 76 for more details. bit 7 6 5 4 3 2 1 0 0x30 (0x50) com0a1 com0a0 com0b1 com0b0 ? ? wgm01 wgm00 tccr0a read/write r/w r/w r/w r/w r r r/w r/w initial value 0 0 0 0 0 0 0 0 table 11-2. compare output mode, non-pwm mode com0a1 com0a0 description 0 0 normal port operation, oc0a disconnected. 0 1 toggle oc0a on compare match 1 0 clear oc0a on compare match 1 1 set oc0a on compare match table 11-3. compare output mode, fast pwm mode (1) com0a1 com0a0 description 0 0 normal port operation, oc0a disconnected. 01 wgm02 = 0: normal port oper ation, oc0a disconnected. wgm02 = 1: toggle oc0a on compare match. 1 0 clear oc0a on compare match, set oc0a at top 1 1 set oc0a on compare match, clear oc0a at top
82 8246a?avr?11/09 attiny2313a/4313 table 11-4 shows the com0a1:0 bit functionality when the wgm02:0 bits are set to phase cor- rect pwm mode. note: 1. a special case occurs when ocr0a equals top and com0a1 is set. in this case, the com- pare match is ignored, but the set or clear is done at top. see ?phase correct pwm mode? on page 78 for more details. ? bits 5:4 ? com0b1:0: compare match output b mode these bits control the output compare pin (oc0b) behavior. if one or both of the com0b1:0 bits are set, the oc0b output overrides the normal po rt functionality of the i/o pin it is connected to. however, note that the data direction r egister (ddr) bit corresponding to the oc0b pin must be set in order to enable the output driver. when oc0b is connected to the pin, the function of the com0b1:0 bits depends on the wgm02:0 bit setting. table 11-5 shows the com0b1:0 bit func tionality when the wgm02:0 bits are set to a normal or ctc mode (non-pwm). table 11-6 shows the com0b1:0 bit functionality when the wgm02:0 bits are set to fast pwm mode. note: 1. a special case occurs when ocr0b equals top and com0b1 is set. in this case, the com- pare match is ignored, but the se t or clear is done at top. see ?fast pwm mode? on page 76 for more details. table 11-4. compare output mode, phase correct pwm mode (1) com0a1 com0a0 description 0 0 normal port operation, oc0a disconnected. 01 wgm02 = 0: normal port oper ation, oc0a disconnected. wgm02 = 1: toggle oc0a on compare match. 10 clear oc0a on compare match when up-counting. set oc0a on compare match when down-counting. 11 set oc0a on compare match when up-counting. clear oc0a on compare match when down-counting. table 11-5. compare output mode, non-pwm mode com0b1 com0b0 description 0 0 normal port operation, oc0b disconnected. 0 1 toggle oc0b on compare match 1 0 clear oc0b on compare match 1 1 set oc0b on compare match table 11-6. compare output mode, fast pwm mode (1) com0b1 com0b0 description 0 0 normal port operation, oc0b disconnected. 01reserved 1 0 clear oc0b on compare match, set oc0b at top 1 1 set oc0b on compare match, clear oc0b at top
83 8246a?avr?11/09 attiny2313a/4313 table 11-7 shows the com0b1:0 bit functionality when the wgm02:0 bits are set to phase cor- rect pwm mode. note: 1. a special case occurs when ocr0b equals top and com0b1 is set. in this case, the com- pare match is ignored, but the set or clear is done at top. see ?phase correct pwm mode? on page 78 for more details. ? bits 3, 2 ? res: reserved bits these bits are reserved bits in the atti ny2313a/4313 and will always read as zero. ? bits 1:0 ? wgm01:0: waveform generation mode combined with the wgm02 bit found in the tccr0b register, these bits control the counting sequence of the counter, the source for maximu m (top) counter value, and what type of wave- form generation to be used, see table 11-8 . modes of operation supported by the timer/counter unit are: normal mode (counter), clear time r on compare match (ctc) mode, and two types of pulse width modulation (pwm) modes (see ?modes of operation? on page 75 ). notes: 1. max = 0xff 2. bottom = 0x00 table 11-7. compare output mode, phase correct pwm mode (1) com0b1 com0b0 description 0 0 normal port operation, ocr0b disconnected. 01reserved 10 clear orc0b on compare match when up-counting. set ocr0b on compare match when down-counting. 11 set ocr0b on compare match when up-counting. clear ocr0b on compare match when down-counting. table 11-8. waveform generation mode bit description mode wgm2 wgm1 wgm0 timer/count er mode of operation top update of ocrx at tov flag set on (1)(2) 0 0 0 0 normal 0xff immediate max 10 0 1 pwm, phase correct 0xff top bottom 2 0 1 0 ctc ocr0a immediate max 3 0 1 1 fast pwm 0xff top max 4100reserved?? ? 51 0 1 pwm, phase correct ocr0a top bottom 6110reserved?? ? 7 1 1 1 fast pwm ocr0a top top
84 8246a?avr?11/09 attiny2313a/4313 11.9.2 tccr0b ? timer/coun ter control register b ? bit 7 ? foc0a: force output compare a the foc0a bit is only active when the wgm bits specify a non-pwm mode. however, for ensuring compatibility with future devices, this bit must be set to zero when tccr0b is written when operating in pwm mode. when writing a logical one to the foc0a bit, an immediate compare match is forced on the wa veform generation unit . the oc0a output is changed according to its com0a1:0 bits setting. note that the foc0a bit is implemented as a strobe. therefore it is the value present in the com0a1:0 bits that determines the effect of the forced compare. a foc0a strobe will not generate any interrupt, nor will it clear the timer in ctc mode using ocr0a as top. the foc0a bit is always read as zero. ? bit 6 ? foc0b: force output compare b the foc0b bit is only active when the wgm bits specify a non-pwm mode. however, for ensuring compatibility with future devices, this bit must be set to zero when tccr0b is written when operating in pwm mode. when writing a logical one to the foc0b bit, an immediate compare match is forced on the wa veform generation unit . the oc0b output is changed according to its com0b1:0 bits setting. note that the foc0b bit is implemented as a strobe. therefore it is the value present in the com0b1:0 bits that determines the effect of the forced compare. a foc0b strobe will not generate any interrupt, nor will it clear the timer in ctc mode using ocr0b as top. the foc0b bit is always read as zero. ? bits 5:4 ? res: reserved bits these bits are reserved bits in the atti ny2313a/4313 and will always read as zero. ? bit 3 ? wgm02: waveform generation mode see the description in the ?tccr0a ? timer/counter control register a? on page 81 . ? bits 2:0 ? cs02:0: clock select the three clock select bits select the clock source to be used by the timer/counter. see table 11-9 on page 85 . bit 7 6 5 4 3 2 1 0 0x33 (0x53) foc0a foc0b ? ? wgm02 cs02 cs01 cs00 tccr0b read/write w w r r r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
85 8246a?avr?11/09 attiny2313a/4313 if external pin modes are used for the timer/counter0, transitions on the t0 pin will clock the counter even if the pin is configured as an outpu t. this feature allows software control of the counting. 11.9.3 tcnt0 ? time r/counter register the timer/counter register gives direct ac cess, both for read and write operations, to the timer/counter unit 8-bit counter. writing to the tcnt0 register blocks (removes) the compare match on the following timer clock. modifying t he counter (tcnt0) while the counter is running, introduces a risk of missing a compare match between tcnt0 and the ocr0x registers. 11.9.4 ocr0a ? output compare register a the output compare register a contains an 8-bi t value that is conti nuously compared with the counter value (tcnt0). a match can be used to generate an output compare interrupt, or to generate a waveform output on the oc0a pin. 11.9.5 ocr0b ? output compare register b the output compare register b contains an 8-bi t value that is conti nuously compared with the counter value (tcnt0). a match can be used to generate an output compare interrupt, or to generate a waveform output on the oc0b pin. table 11-9. clock select bit description cs02 cs01 cs00 description 0 0 0 no clock source (timer/counter stopped) 001clk i/o /(no prescaling) 010clk i/o /8 (from prescaler) 011clk i/o /64 (from prescaler) 100clk i/o /256 (from prescaler) 101clk i/o /1024 (from prescaler) 1 1 0 external clock source on t0 pin. clock on falling edge. 1 1 1 external clock source on t0 pin. clock on rising edge. bit 76543210 0x32 (0x52) tcnt0[7:0] tcnt0 read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 bit 76543210 0x36 (0x56) ocr0a[7:0] ocr0a read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 bit 76543210 0x3c (0x5c) ocr0b[7:0] ocr0b read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000
86 8246a?avr?11/09 attiny2313a/4313 11.9.6 timsk ? timer/counter interrupt mask register ? bit 4 ? res: reserved bit this bit is reserved bit in the attiny 2313a/4313 and will alwa ys read as zero. ? bit 2 ? ocie0b: timer/counter0 output compare match b interrupt enable when the ocie0b bit is written to one, and the i-bit in the status register is set, the timer/counter compare match b interrupt is enab led. the corresponding interrupt is executed if a compare match in timer/counter occurs, i.e., when the ocf0b bi t is set in the timer/counter interrupt flag register ? tifr. ? bit 1 ? toie0: timer/counter0 overflow interrupt enable when the toie0 bit is written to one, and t he i-bit in the status register is set, the timer/counter0 overflow interr upt is enabled. the corresponding interrupt is executed if an overflow in timer/counter0 occu rs, i.e., when the tov0 bit is set in the timer/counter 0 inter- rupt flag register ? tifr. ? bit 0 ? ocie0a: timer/counter0 output compare match a interrupt enable when the ocie0a bit is written to one, and th e i-bit in the status register is set, the timer/counter0 compare match a interrupt is enabled. the corresponding interrupt is executed if a compare match in timer/counter0 occurs, i.e., when the ocf0a bit is set in the timer/counter 0 interrupt flag register ? tifr. 11.9.7 tifr ? timer/counter interrupt flag register ? bit 4 ? res: reserved bit this bit is reserved bit in the attiny 2313a/4313 and will alwa ys read as zero. ? bit 2 ? ocf0b: output compare flag 0 b the ocf0b bit is set when a compare match occurs between the timer/counter and the data in ocr0b ? output compare register0 b. ocf0b is cleared by hardware when executing the cor- responding interrupt handling vector. alternatively, ocf0b is cleared by writing a logic one to the flag. when the i-bit in sreg, ocie0b (tim er/counter compare b match interrupt enable), and ocf0b are set, the timer/counter compare match interrupt is executed. ? bit 1 ? tov0: timer/counter0 overflow flag the bit tov0 is set when an overflow occurs in timer/counter0. tov0 is cleared by hardware when executing the corresponding interrupt handling vector. alternatively, tov0 is cleared by writing a logic one to the flag. when the sreg i-bit, toie0 (timer/count er0 overflow interrupt enable), and tov0 are set, the timer/co unter0 overflow interrupt is executed. bit 76543210 0x39 (0x59) toie1 ocie1a ocie1b ? icie1 ocie0b toie0 ocie0a timsk read/write r/w r/w r/w r r/w r/w r/w r/w initial value00000000 bit 76543210 0x38 (0x58) tov1 ocf1a ocf1b ? icf1 ocf0b tov0 ocf0a tifr read/write r/w r/w r/w r r/w r/w r/w r/w initial value00000000
87 8246a?avr?11/09 attiny2313a/4313 the setting of this flag is dependent of the wgm02:0 bit setting. refer to table 11-8 , ?waveform generation mode bit description? on page 83 . ? bit 0 ? ocf0a: output compare flag 0 a the ocf0a bit is set when a compare match oc curs between the timer/counter0 and the data in ocr0a ? output compare register0 a. ocf0a is cleared by hardware when executing the corresponding interrupt handling vector. alternativ ely, ocf0a is cleared by writing a logic one to the flag. when the i-bit in sreg, ocie0a (t imer/counter0 compare match interrupt enable), and ocf0a are set, the timer/counter0 co mpare match interrupt is executed.
88 8246a?avr?11/09 attiny2313a/4313 12. 16-bit timer/counter1 12.1 features ? true 16-bit design (i.e., allows 16-bit pwm) ? two independent output compare units ? double buffered output compare registers ? one input capture unit ? input capture noise canceler ? clear timer on compare match (auto reload) ? glitch-free, phase correct pulse width modulator (pwm) ? variable pwm period ? frequency generator ? external event counter ? four independent interrupt sources (tov1, ocf1a, ocf1b, and icf1) 12.2 overview the 16-bit timer/counter unit allows accurate program execution timing (event management), wave generation, and signal timing measurement. a simplified block diagram of the 16-bit timer/counter is shown in figure 12-1 . for the actual placement of i/o pins, refer to ?pinout attiny2313a/4313? on page 2 . cpu accessible i/o regis- ters, including i/o bits and i/o pins , are shown in bold. the device- specific i/o register and bit locations are listed in the ?register description? on page 110 . figure 12-1. 16-bit timer/counter block diagram (note:) note: refer to figure 1-1 on page 2 for timer/counter1 pin placement and description. clock select timer/counter data b u s ocrna ocrnb icrn = = tcntn waveform generation waveform generation ocna ocnb noise canceler icpn = fixed top values edge detector control logic = 0 top bottom count clear direction tovn (int.req.) ocna (int.req.) ocnb (int.req.) icfn (int.req.) tccrna tccrnb ( from analog comparator ouput ) tn edge detector ( from prescaler ) clk tn
89 8246a?avr?11/09 attiny2313a/4313 most register and bit references in this sect ion are written in general form. a lower case ?n? replaces the timer/counter number, and a lower case ?x? replaces the output compare unit channel. however, when using the register or bi t defines in a program, the precise form must be used, i.e., tcnt1 for accessing timer/counter1 counter value and so on. 12.2.1 registers the timer/counter (tcnt1), output compare registers (ocr1a/b), and input capture regis- ter (icr1) are all 16-bit registers. special proc edures must be followed when accessing the 16- bit registers. these procedures are described in the section ?accessing 16-bit registers? on page 106 . the timer/counter co ntrol registers (tccr1a/b) are 8-bit registers and have no cpu access restrictions. interrupt requests (abbrevi ated to int.req. in the figure) signals are all visible in the timer interrupt flag register (tifr). all interrupts are in dividually masked with the timer interrupt mask register (timsk). tifr and timsk are not shown in the figure. the timer/counter can be clocked internally, via th e prescaler, or by an external clock source on the t1 pin. the clock select logic block contro ls which clock source and edge the timer/counter uses to increment (or decrement) its value. th e timer/counter is inactive when no clock source is selected. the output from th e clock select logic is referred to as the timer clock (clk t 1 ). the double buffered output compare registers (ocr1a/b) are compared with the timer/coun- ter value at all time. the result of the compare can be used by the waveform generator to generate a pwm or variable frequency output on the output compare pin (oc1a/b). see ?out- put compare units? on page 93. . the compare match event will also set the compare match flag (ocf1a/b) which can be used to generate an output compare interrupt request. the input capture register can c apture the timer/counter value at a given external (edge trig- gered) event on either the input capture pin (icp1) or on the analog comparator pins ( see ?analog comparator? on page 167. ) the input capture unit includes a digital filtering unit (noise canceler) for reducing the chance of capturing noise spikes. the top value, or maximum timer/counter valu e, can in some modes of operation be defined by either the ocr1a register, the icr1 regist er, or by a set of fixed values. when using ocr1a as top value in a pwm mode, the ocr1a register can not be used for generating a pwm output. however, the top value will in this case be do uble buffered allowing the top value to be changed in run time. if a fixed top value is required, the icr1 register can be used as an alternative, freeing the ocr1a to be used as pwm output. 12.2.2 definitions the following definitions are used ex tensively throughout the section: table 12-1. definitions constant description bottom the counter reaches bo ttom when it becomes 0x0000 max the counter reaches its maximum when it becomes 0xffff (decimal 65535) top the counter reaches the top when it becomes equal to the highest value in the count sequence. the top value can be assigned to be one of the fixed values: 0x00ff, 0x01ff, or 0x03ff, or to the value stored in the ocr1a or icr1 register. the assignment depends on the mode of operation
90 8246a?avr?11/09 attiny2313a/4313 12.2.3 compatibility the 16-bit timer/counter has been updated and impr oved from previous versions of the 16-bit avr timer/counter. this 16-bit timer/counter is fully compatible with the earlier version regarding: ? all 16-bit timer/counter related i/o register address locations, including timer interrupt registers. ? bit locations inside all 16-bit timer/counter registers, incl uding timer interrupt registers. ? interrupt vectors. the following control bits have changed name, but have same functionality and register location: ? pwm10 is changed to wgm10. ? pwm11 is changed to wgm11. ? ctc1 is changed to wgm12. the following bits are added to the 16-bit timer/counter control registers: ? foc1a and foc1b are added to tccr1a. ? wgm13 is added to tccr1b. the 16-bit timer/counter has improvements that will affect the compatibility in some special cases. 12.3 timer/counter clock sources the timer/counter can be clocked by an internal or an external clock source. the clock source is selected by the clock select logic which is controlled by the clock select (cs12:0) bits located in the timer/counter control register b (tccr1b). for details on clock sources and prescaler, see ?timer/counter0 and timer/counter1 prescalers? on page 117 . 12.4 counter unit the main part of the 16-bit timer/counter is th e programmable 16-bit bi-directional counter unit. figure 12-2 shows a block diagram of the counter and its surroundings. figure 12-2. counter unit block diagram signal description (internal signals): count increment or decrement tcnt1 by 1. direction select between increment and decrement. clear clear tcnt1 (set all bits to zero). temp (8-bit) data bus (8-bit) tcntn (16-bit counter) tcntnh (8-bit) tcntnl (8-bit) control logic count clear direction tovn (int.req.) clock select top bottom tn edge detector ( from prescaler ) clk tn
91 8246a?avr?11/09 attiny2313a/4313 clk t 1 timer/counter clock. top signalize that tcnt1 has reached maximum value. bottom signalize that tcnt1 has re ached minimum value (zero). the 16-bit counter is mapped into two 8-bit i/o memory locations: counter high (tcnt1h) con- taining the upper eight bits of the counter, and counter low (tcnt1l) containing the lower eight bits. the tcnt1h register can only be indirect ly accessed by the cpu. when the cpu does an access to the tcnt1h i/o location, the cpu accesses the high byte temporary register (temp). the temporary register is updated with the tcnt1h value when the tcnt1l is read, and tcnt1h is updated with the temporary register va lue when tcnt1l is written. this allows the cpu to read or write the entire 16-bit counter value within one clock cycle via the 8-bit data bus. it is important to notice that there are special cases of writing to the tcnt1 register when the counter is counting that will gi ve unpredictable results. the s pecial cases are described in the sections where they are of importance. depending on the mode of operation used, the co unter is cleared, incremented, or decremented at each timer clock (clk t 1 ). the clk t 1 can be generated from an external or internal clock source, selected by the clock select bits (cs12:0). when no clock sour ce is selected (cs12:0 = 0) the timer is stopped. however, the tcnt1 value can be accessed by the cpu, independent of whether clk t 1 is present or not. a cpu write overrides (has priority over) all counter clear or count operations. the counting sequence is determined by the setting of the waveform generation mode bits (wgm13:0) located in the timer/counter control registers a and b (tccr1a and tccr1b). there are close connections between how the counter behaves (counts) and how waveforms are generated on the output compare outputs oc1x. for more details about advanced counting sequences and waveform generation, see ?modes of operation? on page 96 . the timer/counter overflow flag (tov1) is set a ccording to the mode of operation selected by the wgm13:0 bits. tov1 can be us ed for generating a cpu interrupt. 12.5 input capture unit the timer/counter incorporates an input capture unit that can capture external events and give them a time-stamp indicating time of occurrence. the external signal indicating an event, or mul- tiple events, can be applied via the icp1 pin or al ternatively, via the anal og-comparator unit. the time-stamps can then be used to calculate frequenc y, duty-cycle, and other features of the sig- nal applied. alternatively the time-stamps can be used for creating a log of the events. the input capture unit is illustrated by the block diagram shown in figure 12-3 . the elements of the block diagram that are not directly a part of the input capture unit are gray shaded. the small ?n? in register and bit names indicates the timer/counter number.
92 8246a?avr?11/09 attiny2313a/4313 figure 12-3. input capture unit block diagram when a change of the logic level (an event) occurs on the input capture pin (icp1), alternatively on the analog comparator output (aco), and this change confirms to the setting of the edge detector, a capture will be triggered. when a captur e is triggered, the 16-bit value of the counter (tcnt1) is written to the input capture register (icr1). the input capture flag (icf1) is set at the same system clock as the tcnt1 value is copi ed into icr1 register. if enabled (icie1 = 1), the input capture flag generates an input capt ure interrupt. the icf1 flag is automatically cleared when the interrupt is executed. alternativ ely the icf1 flag can be cleared by software by writing a logical one to its i/o bit location. reading the 16-bit value in the input capture register (icr1) is done by first reading the low byte (icr1l) and then the high byte (icr1h). when the low byte is read the high byte is copied into the high byte temporary regi ster (temp). when th e cpu reads the icr1h i/o location it will access the temp register. the icr1 register can only be written when using a waveform generation mode that utilizes the icr1 register for defining the counter?s top value. in these cases the waveform genera- tion mode (wgm13:0) bits must be set before t he top value can be written to the icr1 register. when writing the icr1 re gister the high byte must be written to the icr1h i/o location before the low byte is written to icr1l. for more information on how to access the 16-bit registers refer to ?accessing 16-bit registers? on page 106 . 12.5.1 input capture trigger source the main trigger source for the input capture unit is the input capture pin (icp1). timer/counter1 can alternatively use the analog comparator output as trigger source for the input capture unit. the analog comparator is selected as trigger source by setting the analog comparator input capture (acic) bit in the analog comparator control and status register (acsr). be aware that changing trigger source can trigger a capture. the input capture flag must therefore be cleared after the change. icfn (int.req.) analog comparator write icrn (16-bit register) icrnh (8-bit) noise canceler icpn edge detector temp (8-bit) data bus (8-bit) icrnl (8-bit) tcntn (16-bit counter) tcntnh (8-bit) tcntnl (8-bit) acic* icnc ices aco*
93 8246a?avr?11/09 attiny2313a/4313 both the input capture pin (icp1) and the analog comparator output (aco) inputs are sampled using the same technique as for the t1 pin ( figure 13-1 on page 117 ). the edge detector is also identical. however, when the noise canceler is enabled, additional logic is inserted before the edge detector, which increases t he delay by four system clock cycles. note that the input of the noise canceler and edge detector is always enabl ed unless the timer/counter is set in a wave- form generation mode that uses icr1 to define top. an input capture can be trigger ed by software by controlling the port of the icp1 pin. 12.5.2 noise canceler the noise canceler improves noise immunity by using a simple digita l filtering scheme. the noise canceler input is monitored over four samples, and all four must be equal for changing the output that in turn is used by the edge detector. the noise canceler is enabled by setting the input capture noise canceler (icnc1) bit in timer/counter control register b (tccr1b). when enabled the noise canceler introduces addi- tional four system clock cycles of delay from a change applied to the input, to the update of the icr1 register. the noise canceler uses the sy stem clock and is therefore not affected by the prescaler. 12.5.3 using the input capture unit the main challenge when using the input capture unit is to assign enough processor capacity for handling the incoming events. the time between two events is critical. if the processor has not read the captured valu e in the icr1 register before th e next event occurs, the icr1 will be overwritten with a new value. in this case the result of the ca pture will be incorrect. when using the input capture in terrupt, the icr1 register shoul d be read as early in the inter- rupt handler routine as possible. even though the input capture interrupt has relatively high priority, the maximum interrupt response time is dependent on the maximum number of clock cycles it takes to handle any of the other interrupt requests. using the input capture unit in any mode of operation when the top value (resolution) is actively changed during operation, is not recommended. measurement of an external signal?s duty cycle requires that the trigger edge is changed after each capture. changing the edge sensing must be done as early as possible after the icr1 register has been read. after a change of the edge, the input capture flag (icf1) must be cleared by software (writing a logical one to the i/o bit location). for measuring frequency only, the clearing of the icf1 flag is not r equired (if an interrupt handler is used). 12.6 output compare units the 16-bit comparator continuously compares tcnt1 with the output compare register (ocr1x). if tcnt equals ocr1x the comparator signals a match. a match will set the output compare flag (ocf1x) at the next timer clock cycle. if enabled (ocie1x = 1), the output com- pare flag generates an output compare interrup t. the ocf1x flag is automatically cleared when the interrupt is executed. al ternatively the ocf1x flag can be cleared by software by writ- ing a logical one to its i/o bit location. the waveform generator uses the match signal to generate an output according to operating mode set by the waveform generation mode (wgm13:0) bits and compare output mode (com1x1:0) bits. the top and bottom signals are used by the waveform generator for handling the special cases of the extreme values in some modes of operation ( see ?modes of operation? on page 96. )
94 8246a?avr?11/09 attiny2313a/4313 a special feature of output comp are unit a allows it to define the timer/counter top value (i.e., counter resolution). in addition to the counter resolution, the top val ue defines the period time for waveforms generated by the waveform generator. figure 12-4 shows a block diagram of the output comp are unit. the small ?n? in the register and bit names indicates the device number (n = 1 for timer/counter 1), and the ?x? indicates output compare unit (a/b). the elements of the block di agram that are not directly a part of the output compare unit are gray shaded. figure 12-4. output compare unit, block diagram the ocr1x register is double buffered when using any of the twelve pulse width modulation (pwm) modes. for the normal and clear timer on compare (ctc) modes of operation, the double buffering is disabled. the double buff ering synchronizes the update of the ocr1x com- pare register to either top or bottom of the counting sequence. the synchronization prevents the occurrence of odd-length, non-symmetrical pwm pulses, thereby making the out- put glitch-free. the ocr1x register access may seem complex, but this is not case. when the double buffering is enabled, the cpu has access to the ocr1x buffer register, and if double buffering is dis- abled the cpu will access the ocr1x directly. the content of the ocr1x (buffer or compare) register is only changed by a write operation (the timer/counter does not update this register automatically as the tcnt1 and icr1 register). therefore ocr1x is not read via the high byte temporary register (temp). however, it is a good practice to read the low byte first as when accessing other 16-bit registers. writing the oc r1x registers must be done via the temp reg- ister since the compare of all 16 bits is done continuously. the high byte (ocr1xh) has to be written first. when the high byte i/o location is written by the cpu, the temp register will be updated by the value written. then when the low by te (ocr1xl) is written to the lower eight bits, the high byte will be copied into the upper 8-bits of either the ocr1x bu ffer or ocr1x compare register in the same system clock cycle. ocfnx (int.req.) = (16-bit comparator ) ocrnx buffer (16-bit register) ocrnxh buf. (8-bit) ocnx temp (8-bit) data bus (8-bit) ocrnxl buf. (8-bit) tcntn (16-bit counter) tcntnh (8-bit) tcntnl (8-bit) comnx1:0 wgmn3:0 ocrnx (16-bit register) ocrnxh (8-bit) ocrnxl (8-bit) waveform generator top bottom
95 8246a?avr?11/09 attiny2313a/4313 for more information of how to acce ss the 16-bit registers refer to ?accessing 16-bit registers? on page 106 . 12.6.1 force output compare in non-pwm waveform generation modes, the match output of the comparator can be forced by writing a one to the force output compare (foc1x) bit. forcing compare match will not set the ocf1x flag or reload/clear the timer, but the oc1x pin will be updated as if a real compare match had occurred (the com11:0 bits settings define whether the oc1x pin is set, cleared or toggled). 12.6.2 compare match blocking by tcnt1 write all cpu writes to the tcnt1 register will block any compare match that occurs in the next timer clock cycle, even when the timer is stopped. this feature allows ocr1x to be initialized to the same value as tcnt1 without triggering an inte rrupt when the timer/counter clock is enabled. 12.6.3 using the output compare unit since writing tcnt1 in any mode of operation will block all comp are matches for one timer clock cycle, there are risks involved when changi ng tcnt1 when using any of the output compare channels, independent of whether the timer/counter is running or not. if the value written to tcnt1 equals the ocr1x value, the compare matc h will be missed, resulting in incorrect wave- form generation. do not write the tcnt1 equal to top in pwm modes with variable top values. the compare match for the top will be ignored and the counte r will continue to 0xffff. similarly, do not write the tcnt1 value equal to bottom when the counter is downcounting. the setup of the oc1x should be performed before setting the data direction register for the port pin to output. the easiest way of setting the oc1x value is to use the force output com- pare (foc1x) strobe bits in normal mode. the oc1x register keeps its value even when changing between waveform generation modes. be aware that the com1x1:0 bits are not doubl e buffered together with the compare value. changing the com1x1:0 bits will take effect immediately. 12.7 compare match output unit the compare output mode (com1x1:0) bits have two func tions. the waveform generator uses the com1x1:0 bits for defining the output co mpare (oc1x) state at the next compare match. secondly the com1x1:0 bits control the oc1x pin output source. figure 12-5 shows a simplified schematic of the logic affected by the com1x1:0 bit setting. the i/o registers, i/o bits, and i/o pins in the figure are shown in bold. only the parts of the general i/o port control registers (ddr and port) that are affected by the com1x1:0 bits are shown. when referring to the oc1x state, the reference is for the internal oc1x re gister, not the oc1x pin. if a system reset occur, the oc1x register is reset to ?0?.
96 8246a?avr?11/09 attiny2313a/4313 figure 12-5. compare match output unit, schematic the general i/o port function is overridden by the output compare (oc1x) from the waveform generator if either of the com1x1:0 bits are se t. however, the oc1x pin direction (input or out- put) is still controlled by the data direction register (ddr) for the port pin. the data direction register bit for the oc1x pin (ddr_oc1x) must be set as output before th e oc1x value is visi- ble on the pin. the port overri de function is generally indep endent of the waveform generation mode, but there are some exceptions. refer to table 12-2 , table 12-3 and table 12-4 for details. the design of the output compare pin logic allows initialization of the oc 1x state before the out- put is enabled. note that some com1x1:0 bi t settings are reserved for certain modes of operation. see ?register description? on page 110. the com1x1:0 bits have no effect on the input capture unit. 12.7.1 compare output mode and waveform generation the waveform generator uses the com1x1:0 bits differently in normal, ctc, and pwm modes. for all modes, setting the com1x1:0 = 0 tell s the waveform generator that no action on the oc1x register is to be performed on the next compare match. for compare output actions in the non-pwm modes refer to table 12-2 on page 110 . for fast pwm mode refer to table 12-3 on page 110 , and for phase correct and phase and frequency correct pwm refer to table 12-4 on page 111 . a change of the com1x1:0 bits st ate will have effect at the first compare match after the bits are written. for non-pwm modes, the action can be fo rced to have immediate effect by using the foc1x strobe bits. 12.8 modes of operation the mode of operation, i.e., t he behavior of the timer/counter and the output compare pins, is defined by the combination of the waveform generation mode (wgm13:0) and compare output port ddr dq dq ocnx pin ocnx dq waveform generator comnx1 comnx0 0 1 data b u s focnx clk i/o
97 8246a?avr?11/09 attiny2313a/4313 mode (com1x1:0) bits. the compare output mode bits do not affect the counting sequence, while the waveform generation mode bits do. the com1x1:0 bits control whether the pwm out- put generated should be inverted or not (inverted or non-inverted pwm). for non-pwm modes the com1x1:0 bits control whether the output should be set, cleared or toggle at a compare match ( see ?compare match output unit? on page 95. ) for detailed timing information refer to ?timer/counter timing diagrams? on page 104 . 12.8.1 normal mode the simplest mode of operation is the normal mode (wgm13:0 = 0). in this mode the counting direction is always up (incre menting), and no counter clear is performed. the counter simply overruns when it passes its maximum 16-bit value (max = 0xffff) and then restarts from the bottom (0x0000). in normal operation the timer/counter overflow flag (tov1) will be set in the same timer clock cycle as the tcnt1 beco mes zero. the tov1 flag in this case behaves like a 17th bit, except that it is only set, not cleared. however, combined with the timer overflow interrupt that automatically clears the tov1 flag , the timer resolution ca n be increased by soft- ware. there are no special cases to consider in the normal mode, a new counter value can be written anytime. the input capture unit is easy to use in normal mode. however, observe that the maximum interval between the external events must not exceed the resolution of the counter. if the interval between events are too long, the timer overflow interrupt or the prescaler must be used to extend the resolution for the capture unit. the output compare units can be used to generat e interrupts at some given time. using the output compare to gene rate waveforms in norm al mode is not recommended, since this will occupy too much of the cpu time. 12.8.2 clear timer on compare match (ctc) mode in clear timer on compare or ctc mode (wgm13:0 = 4 or 12), the ocr1a or icr1 register are used to manipulate the counter resolution. in ctc mode the counter is cleared to zero when the counter value (tcnt1) matches either the oc r1a (wgm13:0 = 4) or the icr1 (wgm13:0 = 12). the ocr1a or icr1 define the top value for the counter, hence also its resolution. this mode allows greater control of the compare match ou tput frequency. it also simplifies the opera- tion of counting external events. the timing diagram for the ctc mode is shown in figure 12-6 . the counter value (tcnt1) increases until a compare match occurs with ei ther ocr1a or icr1, and then counter (tcnt1) is cleared.
98 8246a?avr?11/09 attiny2313a/4313 figure 12-6. ctc mode, timing diagram an interrupt can be generated at each time the counter value reaches the top value by either using the ocf1a or icf1 flag according to the re gister used to define the top value. if the inter- rupt is enabled, the interrupt handler routine ca n be used for updating the top value. however, changing the top to a value close to bottom w hen the counter is running with none or a low prescaler value must be done with care since the ctc mode does not have the double buffering feature. if the new value written to ocr1a or icr1 is lowe r than the current value of tcnt1, the counter will miss the compare matc h. the counter will th en have to count to its maximum value (0xffff) and wrap around starting at 0x0000 before the compare match can occur. in many cases this feature is not desirable. an alternativ e will then be to use the fast pwm mode using ocr1a for defining top (wgm13:0 = 15) si nce the ocr1a then will be double buffered. for generating a waveform output in ctc mode, the ocfa output can be set to toggle its logical level on each compare match by setting the compare output mode bits to toggle mode (com1a1:0 = 1). the ocf1a value will not be visi ble on the port pin unless the data direction for the pin is set to output (ddr_ocf1a = 1). the waveform generated will have a maximum frequency of f oc 1 a = f clk_i/o /2 when ocr1a is set to zero (0x0000). the waveform frequency is defined by the following equation: the n variable represents the prescaler factor (1, 8, 64, 256, or 1024). as for the normal mode of operation, the tov1 flag is set in the same time r clock cycle that the counter counts from max to 0x0000. 12.8.3 fast pwm mode the fast pulse width modulation or fast pwm mode (wgm13:0 = 5, 6, 7, 14, or 15) provides a high frequency pwm waveform generation option. the fast pwm differs from the other pwm options by its single-slope operation. the counter counts from bottom to top then restarts from bottom. in non-inverting compare output mode, the output compare (oc1x) is set on the compare match between tcnt1 and ocr1x, and cleared at top. in inverting compare output mode output is cleared on compare match and set at top. due to the single-slope oper- ation, the operating frequency of the fast pwm mo de can be twice as high as the phase correct and phase and frequency correct pwm modes that use dual-slope operation. this high fre- quency makes the fast pwm mode well suited fo r power regulation, re ctification, and dac applications. high frequency allows physically sm all sized external com ponents (coils, capaci- tors), hence reduces total system cost. tcntn ocna (toggle) ocna interrupt flag set or icfn interrupt flag set (interrupt on top) 1 4 period 2 3 (comna1:0 = 1) f ocna f clk_i/o 2 n 1 ocrna + () ?? --------------------------------------------------- =
99 8246a?avr?11/09 attiny2313a/4313 the pwm resolution for fast pwm can be fixed to 8- , 9-, or 10-bit, or defined by either icr1 or ocr1a. the minimum resolution allowed is 2-bit (icr1 or ocr1a set to 0x0003), and the max- imum resolution is 16-bit (icr1 or ocr1a set to max). the pwm resolution in bits can be calculated by using the following equation: in fast pwm mode the co unter is incremented until the counter value matches either one of the fixed values 0x00ff, 0x01ff, or 0x03ff (wgm13:0 = 5, 6, or 7), the value in icr1 (wgm13:0 = 14), or the value in ocr1a (wgm13:0 = 15). th e counter is then cleared at the following timer clock cycle. the timing diagram fo r the fast pwm mode is shown in figure 12-7 . the figure shows fast pwm mode when ocr1a or icr1 is us ed to define top. the tcnt1 value is in the timing diagram shown as a histogram for illu strating the single-slope operation. the diagram includes non-inverted and inverted pwm outputs. the small horizontal line marks on the tcnt1 slopes represent compare matc hes between ocr1x and tcnt1. the oc1x interrupt flag will be set when a compare match occurs. figure 12-7. fast pwm mode, timing diagram the timer/counter overflow flag (tov1) is set each time the counter reaches top. in addition the ocf1a or icf1 flag is set at the same timer clock cycle as tov1 is set when either ocr1a or icr1 is used for defining the top value. if o ne of the interrupts are enabled, the interrupt han- dler routine can be used for updating the top and compare values. when changing the top value the program must ensure that the new top value is higher or equal to the value of all of the compare registers. if the top value is lower than any of the compare registers, a compare match will never occur between the tcnt1 and the ocr1x. note that when using fixed top values the unused bits are masked to zero when any of the ocr1x registers are written. the procedure for updating icr1 differs from updating ocr1a when used for defining the top value. the icr1 register is not double buffered. this means that if icr1 is changed to a low value when the counter is running with none or a lo w prescaler value, there is a risk that the new icr1 value written is lower than the current va lue of tcnt1. the result will then be that the counter will miss the compare matc h at the top value. the counter will then have to count to the max value (0xffff) and wrap around starting at 0x0000 before the compare match can occur. r fpwm top 1 + () log 2 () log ---------------------------------- - = tcntn ocrnx/top update and tovn interrupt flag set and ocna interrupt flag set or icfn interrupt flag set (interrupt on top) 1 7 period 2 3 4 5 6 8 ocnx ocnx (comnx1:0 = 2) (comnx1:0 = 3)
100 8246a?avr?11/09 attiny2313a/4313 the ocr1a register however, is double buffered. this feature allows the ocr1a i/o location to be written anytime. when the ocr1a i/o location is written the value written will be put into the ocr1a buffer register. th e ocr1a compare register will th en be updated with the value in the buffer register at the next timer clo ck cycle the tcnt1 matches top. the update is done at the same timer clock cycle as the tcnt 1 is cleared and the tov1 flag is set. using the icr1 register for defining top work s well when using fixed top values. by using icr1, the ocr1a register is free to be used for generating a pwm output on oc1a. however, if the base pwm frequency is actively change d (by changing the top value), using the ocr1a as top is clearly a better choice due to its double buffer feature. in fast pwm mode, the compare units allow generation of pwm waveforms on the oc1x pins. setting the com1x1:0 bits to two will produce a non-inverted pwm and an inverted pwm output can be generated by setting the com1x1:0 to three (see table 12-2 on page 110 ). the actual oc1x value will only be visible on the port pin if th e data direction for the po rt pin is set as output (ddr_oc1x). the pwm waveform is generated by setting (or clearing) the oc1x register at the compare match between ocr1x and tcnt1, and clearing (or setting) the oc1x register at the timer clock cycle the counter is cleared (changes from top to bottom). the pwm frequency for the output can be calculated by the following equation: the n variable represents the prescaler divider (1, 8, 64, 256, or 1024). the extreme values for the ocr1x register re presents special cases when generating a pwm waveform output in the fast pwm mode. if the ocr1x is set equal to bottom (0x0000) the out- put will be a narrow spike for eac h top+1 timer clock cycle. se tting the ocr1x equal to top will result in a const ant high or low output (depending on the polarity of the output set by the com1x1:0 bits.) a frequency (with 50% duty cycle) waveform out put in fast pwm mode can be achieved by set- ting ocf1a to toggle its logical level on each compare match (com1a1:0 = 1). the waveform generated will have a maximum frequency of f oc 1 a = f clk_i/o /2 when ocr1a is set to zero (0x0000). this feature is similar to the ocf1 a toggle in ctc mode, except the double buffer feature of the output compare unit is enabled in the fast pwm mode. 12.8.4 phase correct pwm mode the phase correct pulse width modulation or phase correct pwm mode (wgm13:0 = 1, 2, 3, 10, or 11) provides a high resolution phase correct pwm waveform generation option. the phase correct pwm mode is, like the phase and frequency correct pwm mode, based on a dual- slope operation. the counter counts repeatedly from bottom (0x0000) to top and then from top to bottom. in non-inverting compare output mode, the output compare (oc1x) is cleared on the compare match between tcnt1 and ocr1x while upcounting, and set on the compare match while downcounting. in inverting output compare mode, the operation is inverted. the dual-slope operation has lower maximum operation frequency than single slope operation. however, due to the symmetric feat ure of the dual-slope pwm modes, these modes are preferred for motor control applications. the pwm resolution for the phase correct pwm mode can be fixed to 8-, 9-, or 10-bit, or defined by either icr1 or ocr1a. the minimum resolution allowed is 2-bit (icr1 or ocr1a set to f ocnxpwm f clk_i/o n 1 top + () ? ---------------------------------- - =
101 8246a?avr?11/09 attiny2313a/4313 0x0003), and the maximum resolution is 16-bit (icr1 or ocr1a set to max). the pwm resolu- tion in bits can be calculated by using the following equation: in phase correct pwm mode the counter is incr emented until the counter value matches either one of the fixed values 0x00ff, 0x01ff, or 0x 03ff (wgm13:0 = 1, 2, or 3), the value in icr1 (wgm13:0 = 10), or the value in ocr1a (wgm13:0 = 11). the counter has then reached the top and changes the count direct ion. the tcnt1 value will be equa l to top for one timer clock cycle. the timing diagram for the phase correct pwm mode is shown on figure 12-8 on page 101 . the figure shows phase correct pwm mode w hen ocr1a or icr1 is used to define top. the tcnt1 value is in the timing diagram shown as a histogram for illu strating the dual-slope operation. the diagram includes non-inverted and inverted pwm outputs. the small horizontal line marks on the tcnt1 slopes represent compare matches between ocr1x and tcnt1. the oc1x interrupt flag will be set when a compare match occurs. figure 12-8. phase correct pwm mode, timing diagram the timer/counter overflow flag (tov1) is se t each time the counter reaches bottom. when either ocr1a or icr1 is used for defining the to p value, the ocf1a or ic f1 flag is set accord- ingly at the same timer clock cycle as the ocr1x registers are updated with the double buffer value (at top). the interr upt flags can be used to generate an interrupt each time the counter reaches the top or bottom value. when changing the top value the program must ensure that the new top value is higher or equal to the value of all of the compare registers. if the top value is lower than any of the compare registers, a compare match will never occur between the tcnt1 and the ocr1x. note that when using fixed top values, the unus ed bits are masked to zero when any of the ocr1x registers are written. as the third period shown in figure 12-8 illustrates, changing the top actively while the timer/counter is runni ng in the phase correct mode can result in an unsymmetrical output. the reason for this can be found in the time of update of the ocr1x reg- ister. since the ocr1x update occurs at top, the pwm period starts and ends at top. this r pcpwm top 1 + () log 2 () log ---------------------------------- - = ocrnx/top update and ocna interrupt flag set or icfn interrupt flag set (interrupt on top) 1 2 3 4 tovn interrupt flag set (interrupt on bottom) tcntn period ocnx ocnx (comnx1:0 = 2) (comnx1:0 = 3)
102 8246a?avr?11/09 attiny2313a/4313 implies that the length of the falling slope is determined by the previous top value, while the length of the rising slope is determined by th e new top value. when thes e two values differ the two slopes of the period will differ in length. the difference in length gives the unsymmetrical result on the output. it is recommended to use the phase and frequency correct mode instead of the phase correct mode when changing the top value while the timer/counter is running. when using a static top value there are practically no differ ences between the two modes of operation. in phase correct pwm mode, the compare units allow generation of pwm waveforms on the oc1x pins. setting the com1x1:0 bits to tw o will produce a non-inverte d pwm and an inverted pwm output can be generated by sett ing the com1x1:0 to three (see table 12-3 on page 110 ). the actual oc1x value will only be visible on the port pin if the data direction for the port pin is set as output (ddr_oc1x). the pwm waveform is generated by setting (or clearing) the oc1x register at the compare match between ocr1x and tcnt1 when the counter increments, and clearing (or setting) the oc1x register at compare match between ocr1x and tcnt1 when the counter decrements. the pw m frequency for the output when using phase correct pwm can be calculated by the following equation: the n variable represents the prescaler divider (1, 8, 64, 256, or 1024). the extreme values for the ocr1x register re present special cases when generating a pwm waveform output in the phase correct pwm mode. if the ocr1x is set equal to bottom the output will be continuously low and if set equal to top the output will be continuously high for non-inverted pwm mode. for in verted pwm the output will have the opposite logic values. 12.8.5 phase and frequency correct pwm mode the phase and frequency correct pulse width modulation, or phase and frequency correct pwm mode (wgm13:0 = 8 or 9) provides a high reso lution phase and frequency correct pwm wave- form generation option. the phase and frequency correct pwm mode is, like the phase correct pwm mode, based on a dual-slope operation. the counter counts repeatedly from bottom (0x0000) to top and then from top to bottom. in non-inverting compare output mode, the output compare (oc1x) is cleared on the co mpare match between tcnt1 and ocr1x while upcounting, and set on the compare match while downcounting. in inverting compare output mode, the operation is inverted. the dual-slope operation gives a lower maximum operation fre- quency compared to the single-slope operation. howe ver, due to the symme tric feature of the dual-slope pwm modes, these modes are preferred for motor control applications. the main difference between the phase correct, and the phase and frequency correct pwm mode is the time the ocr1x register is up dated by the ocr1x buffer register, (see figure 12- 8 on page 101 and figure 12-9 on page 103 ). the pwm resolution for the phase and frequency correct pwm mode can be defined by either icr1 or ocr1a. the minimum resolution allowed is 2-bit (icr1 or ocr1a set to 0x0003), and the maximum resolution is 16-bit (icr1 or ocr1 a set to max). the pwm re solution in bits can be calculated using the following equation: f ocnxpcpwm f clk_i/o 2 ntop ?? --------------------------- - = r pfcpwm top 1 + () log 2 () log ---------------------------------- - =
103 8246a?avr?11/09 attiny2313a/4313 in phase and frequency correct pwm mode the counter is incremented until the counter value matches either the value in icr1 (wgm13:0 = 8), or the value in ocr1a (wgm13:0 = 9). the counter has then reac hed the top and ch anges the count di rection. the tcnt1 value will be equal to top for one timer clock cycle. the timi ng diagram for the phase correct and frequency correct pwm mode is shown on figure 12-9 on page 103 . the figure shows phase and fre- quency correct pwm mode when ocr1a or icr1 is used to define top. the tcnt1 value is in the timing diagram shown as a histogram for il lustrating the dual-slope operation. the diagram includes non-inverted and inverted pwm outputs. the small horizontal line marks on the tcnt1 slopes represent compare matc hes between ocr1x and tcnt1. the oc1x interrupt flag will be set when a compare match occurs. figure 12-9. phase and frequency correct pwm mode, timing diagram the timer/counter overflow flag (tov1) is set at the same timer cloc k cycle as the ocr1x registers are updated with the double buffer value (at bottom). when either ocr1a or icr1 is used for defining the top value, the ocf1a or icf1 flag set when tcnt1 has reached top. the interrupt flags can then be used to generate an interrupt each time the counter reaches the top or bottom value. when changing the top value the program must ensure that the new top value is higher or equal to the value of all of the compare registers. if the top value is lower than any of the compare registers, a compare match will neve r occur between the tcnt1 and the ocr1x. as figure 12-9 shows the output generated is, in contra st to the phase correct mode, symmetri- cal in all periods. since the ocr1x registers are updated at bottom, the length of the rising and the falling slopes will always be equal. this gives symmetrical output pulses and is therefore frequency correct. using the icr1 register for defining top work s well when using fixed top values. by using icr1, the ocr1a register is free to be used for generating a pwm output on oc1a. however, if the base pwm frequency is actively change d by changing the top value, using the ocr1a as top is clearly a better choice due to its double buffer feature. in phase and frequency correct pwm mode, the compare units allow generation of pwm wave- forms on the oc1x pins. settin g the com1x1:0 bits to two will produce a non-inverted pwm and ocrnx/top updateand tovn interrupt flag set (interrupt on bottom) ocna interrupt flag set or icfn interrupt flag set (interrupt on top) 1 2 3 4 tcntn period ocnx ocnx (comnx1:0 = 2) (comnx1:0 = 3)
104 8246a?avr?11/09 attiny2313a/4313 an inverted pwm output can be generated by setting the com1x1:0 to three (see table 12-4 on page 111 ). the actual oc1fx value will only be visible on the port pin if th e data direction for the port pin is set as output ( ddr_ocf1x). the pwm waveform is gen erated by setting (or clearing) the ocf1x register at the compare match be tween ocr1x and tcnt1 when the counter incre- ments, and clearing (or setting) the ocf1x re gister at compare match between ocr1x and tcnt1 when the counter decrem ents. the pwm frequency for the output when using phase and frequency correct pwm can be calculated by the following equation: the n variable represents the prescaler divider (1, 8, 64, 256, or 1024). the extreme values for the ocr1x register re presents special cases when generating a pwm waveform output in the phase correct pwm mode. if the ocr1x is set equal to bottom the output will be continuously low and if set equal to top the output will be set to high for non- inverted pwm mode. for inverted pwm the ou tput will have the opposite logic values. 12.9 timer/counter timing diagrams the timer/counter is a synchronous design and the timer clock (clk t1 ) is therefore shown as a clock enable signal in the following figures. the figures include information on when interrupt flags are set, and when the ocr1x register is updated with the ocr1x buffer value (only for modes utilizing double buffering). figure 12-10 shows a timing diagram fo r the setting of ocf1x. figure 12-10. timer/counter timing diagram, setting of ocf1x, no prescaling figure 12-11 shows the same timing data, but with the prescaler enabled. f ocnxpfcpwm f clk_i/o 2 ntop ?? --------------------------- - = clk tn (clk i/o /1) ocfnx clk i/o ocrnx tcntn ocrnx value ocrnx - 1 ocrnx ocrnx + 1 ocrnx + 2
105 8246a?avr?11/09 attiny2313a/4313 figure 12-11. timer/counter timing diagram, setting of ocf1x, with prescaler (f clk_i/o /8) figure 12-12 on page 105 shows the count sequence close to top in various modes. when using phase and frequency correct pwm mode t he ocr1x register is updated at bottom. the timing diagrams will be the same, but to p should be replaced by bottom, top-1 by bottom+1 and so on. the same renaming applies for modes that set the tov1 flag at bottom. figure 12-12. timer/counter timing diagram, no prescaling figure 12-13 shows the same timing data, but with the prescaler enabled. ocfnx ocrnx tcntn ocrnx value ocrnx - 1 ocrnx ocrnx + 1 ocrnx + 2 clk i/o clk tn (clk i/o /8) tovn (fpwm) and icfn (if used as top) ocrnx (update at top) tcntn (ctc and fpwm) tcntn (pc and pfc pwm) top - 1 top top - 1 top - 2 old ocrnx value new ocrnx value top - 1 top bottom bottom + 1 clk tn (clk i/o /1) clk i/o
106 8246a?avr?11/09 attiny2313a/4313 figure 12-13. timer/counter timing diagram, with prescaler (f clk_i/o /8) 12.10 accessing 16-bit registers the tcnt1, ocr1a/b, and icr1 are 16-bit regist ers that can be accessed by the avr cpu via the 8-bit data bus. the 16-bit register must be byte accessed using two read or write operations. each 16-bit timer has a single 8-bit register for temporary storing of the high byte of the 16-bit access. the same temporary register is shared between all 16-bit registers within each 16-bit timer. accessing the low byte triggers the 16-bit read or write operation. when the low byte of a 16-bit register is written by the cpu, the high byte stored in the temporary register, and the low byte written are both copied into the 16-bit regist er in the same clock cycle. when the low byte of a 16-bit register is read by the cpu, the high by te of the 16-bit register is copied into the tempo- rary register in the same clock cycle as the low byte is read. not all 16-bit accesses uses the temporary regi ster for the high byte. reading the ocr1a/b 16- bit registers does not involve using the temporary register. to do a 16-bit write, the high byte must be written before the low byte. for a 16-bit read, the low byte must be read before the high byte. the following code examples show how to acce ss the 16-bit timer registers assuming that no interrupts updates the temporary register. the same principle can be used directly for accessing tovn (fpwm) and icf n (if used as top) ocrnx (update at top) tcntn (ctc and fpwm) tcntn (pc and pfc pwm) top - 1 top top - 1 top - 2 old ocrnx value new ocrnx value top - 1 top bottom bottom + 1 clk i/o clk tn (clk i/o /8)
107 8246a?avr?11/09 attiny2313a/4313 the ocr1a/b and icr1 registers. note that when using ?c?, the compiler handles the 16-bit access. note: 1. see ?code examples? on page 6. the assembly code example returns the t cnt1 value in the r17:r16 register pair. it is important to notice that accessing 16-bit registers are atomic operations. if an interrupt occurs between the two instructions accessing the 16-bit register, and the interrupt code updates the temporary register by accessing the sa me or any other of the 16-bit timer registers, then the result of the access outside the interrupt will be corrupted. therefore, when both the main code and the interrupt code update the temporary register, the main code must disable the interrupts during the 16-bit access. assembly code examples (1) ... ; set tcnt 1 to 0x01ff ldi r17,0x01 ldi r16,0xff out tcnt 1 h,r17 out tcnt 1 l,r16 ; read tcnt 1 into r17:r16 in r16,tcnt 1 l in r17,tcnt 1 h ... c code examples (1) unsigned int i; ... /* set tcnt 1 to 0x01ff */ tcnt 1 = 0x1ff; /* read tcnt 1 into i */ i = tcnt 1 ; ...
108 8246a?avr?11/09 attiny2313a/4313 the following code examples show how to do an atomic read of the tcnt1 register contents. reading any of the ocr1a/b or icr1 register s can be done by using the same principle. note: 1. see ?code examples? on page 6. the assembly code example returns the t cnt1 value in the r17:r16 register pair. assembly code example (1) tim16_readtcnt 1 : ; save global interrupt flag in r18,sreg ; disable interrupts cli ; read tcnt 1 into r17:r16 in r16,tcnt 1 l in r17,tcnt 1 h ; restore global interrupt flag out sreg,r18 ret c code example (1) unsigned int tim16_readtcnt 1 ( void ) { unsigned char sreg; unsigned int i; /* save global interrupt flag */ sreg = sreg; /* disable interrupts */ __disable_interrupt(); /* read tcnt 1 into i */ i = tcnt 1 ; /* restore global interrupt flag */ sreg = sreg; return i; }
109 8246a?avr?11/09 attiny2313a/4313 the following code examples show how to do an at omic write of the tcnt1 register contents. writing any of the ocr1a/b or icr1 register s can be done by using the same principle. note: 1. see ?code examples? on page 6. the assembly code example requires that the r17: r16 register pair contains the value to be writ- ten to tcnt1. 12.10.1 reusing the temporary high byte register if writing to more than one 16-bit register where the high byte is the same for all registers written, then the high byte only needs to be written once. however, note that the same rule of atomic operation described previously also applies in this case. assembly code example (1) tim16_writetcnt 1 : ; save global interrupt flag in r18,sreg ; disable interrupts cli ; set tcnt 1 to r17:r16 out tcnt 1 h,r17 out tcnt 1 l,r16 ; restore global interrupt flag out sreg,r18 ret c code example (1) void tim16_writetcnt 1 ( unsigned int i ) { unsigned char sreg; unsigned int i; /* save global interrupt flag */ sreg = sreg; /* disable interrupts */ __disable_interrupt(); /* set tcnt 1 to i */ tcnt 1 = i; /* restore global interrupt flag */ sreg = sreg; }
110 8246a?avr?11/09 attiny2313a/4313 12.11 register description 12.11.1 tccr1a ? timer/coun ter1 control register a ? bit 7:6 ? com1a1:0: compare output mode for channel a ? bit 5:4 ? com1b1:0: compare output mode for channel b the com1a1:0 and com1b1:0 control the out put compare pins (oc1a and oc1b respec- tively) behavior. if one or both of the com1a1:0 bits are written to one, the oc1a output overrides the normal port functionality of the i/o pin it is connected to. if one or both of the com1b1:0 bit are written to one, the oc1b output overrides the normal port functionality of the i/o pin it is connected to. however, note that the data direction register (ddr) bit correspond- ing to the oc1a or oc1b pin must be set in order to enable the output driver. when the oc1a or oc1b is connected to the pin, the function of the com1x1:0 bits is depen- dent of the wgm13:0 bits setting. table 12-2 shows the com1x1:0 bit functionality when the wgm13:0 bits are set to a normal or a ctc mode (non-pwm). table 12-3 shows the com1x1:0 bit functionality when the wgm13:0 bits are set to the fast pwm mode. note: 1. a special case occurs when ocr1a/oc r1b equals top and com1a1/com1b1 is set. in this case the compare match is ignored, but the set or clear is done at top. see ?fast pwm mode? on page 98. for more details. bit 7 6 5 4 3210 0x2f (0x4f) com1a1 com1a0 com1b1 com1b0 ? ? wgm11 wgm10 tccr1a read/write r/w r/w r/w r/w r r r/w r/w initial value 0 0 0 0 0 0 0 0 table 12-2. compare output mode, non-pwm com1a1/com1b1 com1a 0/com1b0 description 00 normal port operation, oc1a/oc1b disconnected. 0 1 toggle oc1a/oc1b on compare match. 10 clear oc1a/oc1b on compare match (set output to low level). 11 set oc1a/oc1b on compare match (set output to high level). table 12-3. compare output mode, fast pwm (1) com1a1/com1b1 com1a 0/com1b0 description 00 normal port operation, oc1a/oc1b disconnected. 01 wgm13=0: normal port operation, oc1a/oc1b disconnected. wgm13=1: toggle oc1a on compare match, oc1b reserved. 10 clear oc1a/oc1b on compare match, set oc1a/oc1b at top 11 set oc1a/oc1b on compare match, clear oc1a/oc1b at top
111 8246a?avr?11/09 attiny2313a/4313 table 12-4 shows the com1x1:0 bit functionality wh en the wgm13:0 bits are set to the phase correct or the phase and frequency correct, pwm mode. note: 1. a special case occurs when ocr1a/ ocr1b equals top and com1a1/com1b1 is set. see ?phase correct pwm mode? on page 100. for more details. ? bit 1:0 ? wgm11:0: waveform generation mode combined with the wgm13:2 bits found in the tc cr1b register, these bits control the counting sequence of the counter, the source for maximu m (top) counter value, and what type of wave- form generation to be used, see table 12-5 . modes of operation supported by the timer/counter unit are: normal mode (counter), clear timer on compare match (ctc) mode, and three types of pulse width modulation (pwm) modes. ( see ?modes of operation? on page 96. ). table 12-4. compare output mode, phase correct and phase and frequency correct pwm (1) com1a1/com1b1 com1a 0/com1b0 description 00 normal port operation, oc1a/oc1b disconnected. 01 wgm13=0: normal port operation, oc1a/oc1b disconnected. wgm13=1: toggle oc1a on compare match, oc1b reserved. 10 clear oc1a/oc1b on compare match when up- counting. set oc1a/oc1b on compare match when downcounting. 11 set oc1a/oc1b on compare match when up- counting. clear oc1a/oc1b on compare match when downcounting.
112 8246a?avr?11/09 attiny2313a/4313 note: 1. the ctc1 and pwm11:0 bit definition names are obsolete. use the wgm12:0 definitions. however, the functionality and location of these bits are compatible with previous versions of the timer. 12.11.2 tccr1b ? timer/coun ter1 control register b ? bit 7 ? icnc1: input capture noise canceler setting this bit (to one) activates the input capt ure noise canceler. when the noise canceler is activated, the input from the inpu t capture pin (icp1) is filtered. the filter function requires four successive equal valued samples of the icp1 pin for changing its output. the input capture is therefore delayed by four oscillator cycles when the noise canceler is enabled. ? bit 6 ? ices1: input capture edge select this bit selects which edge on the input capture pin (icp1) that is used to trigger a capture event. when the ices1 bit is written to zero, a falling (negative) edge is used as trigger, and when the ices1 bit is written to one, a risi ng (positive) edge w ill trigger the capture. when a capture is triggered according to the ices1 setting, the counter value is copied into the input capture register (icr1). the event will also set the input capture flag (icf1), and this can be used to cause an input capture in terrupt, if this in terrupt is enabled. table 12-5. waveform generation mode bit description (1) mode wgm13 wgm12 (ctc1) wgm11 (pwm11) wgm10 (pwm10) timer/counter mode of operation top update of ocr1 x at tov1 flag set on 0 0 0 0 0 normal 0xffff immediate max 1 0 0 0 1 pwm, phase correct, 8-bit 0x00ff top bottom 2 0 0 1 0 pwm, phase correct, 9-bit 0x01ff top bottom 3 0 0 1 1 pwm, phase correct, 10-bit 0x03ff top bottom 4 0 1 0 0 ctc ocr1a immediate max 5 0 1 0 1 fast pwm, 8-bit 0x00ff top top 6 0 1 1 0 fast pwm, 9-bit 0x01ff top top 7 0 1 1 1 fast pwm, 10-bit 0x03ff top top 81000 pwm, phase and frequency correct icr1 bottom bottom 91001 pwm, phase and frequency correct ocr1a bottom bottom 10 1 0 1 0 pwm, phase correct icr1 top bottom 11 1 0 1 1 pwm, phase correct ocr1a top bottom 12 1 1 0 0 ctc icr1 immediate max 13 1 1 0 1 (reserved) ? ? ? 14 1 1 1 0 fast pwm icr1 top top 15 1 1 1 1 fast pwm ocr1a top top bit 7654 3210 0x2e (0x4e) icnc1 ices1 ? wgm13 wgm12 cs12 cs11 cs10 tccr1b read/write r/w r/w r r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
113 8246a?avr?11/09 attiny2313a/4313 when the icr1 is used as top value (see description of the wgm13:0 bits located in the tccr1a and the tccr1b register), the icp1 is disconnected and consequently the input cap- ture function is disabled. ? bit 5 ? reserved bit this bit is reserved for future use. for ensuring compatibility with future de vices, this bit must be written to zero when tccr1b is written. ? bit 4:3 ? wgm13:2: waveform generation mode see tccr1a register description. ? bit 2:0 ? cs12:0: clock select the three clock select bits select the clock source to be used by the timer/counter, see figure 12-10 on page 104 and figure 12-11 on page 105 . if external pin modes are used for the timer/counter1, transitions on the t1 pin will clock the counter even if the pin is configured as an outpu t. this feature allows software control of the counting. 12.11.3 tccr1c ? timer/coun ter1 control register c ? bit 7 ? foc1a: force output compare for channel a ? bit 6 ? foc1b: force output compare for channel b the foc1a/foc1b bits are only active when the wgm13:0 bits specifies a non-pwm mode. however, for ensuring compatibilit y with future devices, these bits must be set to zero when tccr1a is written when operating in a pw m mode. when writing a logical one to the foc1a/foc1b bit, an immediate compare matc h is forced on the wave form generation unit. the oc1a/oc1b output is changed according to its com1x1:0 bits setting. note that the foc1a/foc1b bits are implemented as strobes. therefore it is the value present in the com1x1:0 bits that determine t he effect of the forced compare. table 12-6. clock select bit description cs12 cs11 cs10 description 0 0 0 no clock source (timer/counter stopped). 001clk i/o /1 (no prescaling) 010clk i/o /8 (from prescaler) 011clk i/o /64 (from prescaler) 100clk i/o /256 (from prescaler) 101clk i/o /1024 (from prescaler) 1 1 0 external clock source on t1 pin. clock on falling edge. 1 1 1 external clock source on t1 pin. clock on rising edge. bit 7654 3210 0x22 (ox42) foc1a foc1b ? ? ? ? ? ? tccr1c read/write w w r r r r r r initial value 0 0 0 0 0 0 0 0
114 8246a?avr?11/09 attiny2313a/4313 a foc1a/foc1b strobe will not generate any interrupt nor will it clear t he timer in clear timer on compare match (ctc) mode using ocr1a as top. the foc1a/foc1b bits are always read as zero. 12.11.4 tcnt1h and tcnt1l ? timer/counter1 the two timer/counter i/o locations (tcnt1h and tcnt1l , combined tcnt1) give direct access, both for read and for write operations, to the timer/counter unit 16-bit counter. to ensure that both the high and low bytes are read and written simultaneously when the cpu accesses these registers, the access is perfo rmed using an 8-bit temporary high byte register (temp). this temporary register is shared by all the other 16-bit registers. see ?accessing 16-bit registers? on page 106. modifying the counter (tcnt1) while the counte r is running introduces a risk of missing a com- pare match between tcnt1 and one of the ocr1x registers. writing to the tcnt1 register blocks (removes ) the compare match on the following timer clock for all compare units. 12.11.5 ocr1ah and ocr1al ? ou tput compare register 1 a 12.11.6 ocr1bh and ocr1bl ? ou tput compare register 1 b the output compare registers contain a 16-bit value that is continuo usly compared with the counter value (tcnt1). a match can be used to generate an output compare interrupt, or to generate a waveform output on the oc1x pin. the output compare registers are 16-bit in size. to ensure that both the high and low bytes are written simultaneously when the cp u writes to these registers, the access is performed using an 8-bit temporary high byte register (temp). this te mporary register is shared by all the other 16- bit registers. see ?accessing 16-bit registers? on page 106. 12.11.7 icr1h and icr1l ? input capture register 1 bit 76543210 0x2d (0x4d) tcnt1[15:8] tcnt1h 0x2c (0x4c) tcnt1[7:0] tcnt1l read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 bit 76543210 0x2b (0x4b) ocr1a[15:8] ocr1ah 0x2a (0x4a) ocr1a[7:0] ocr1al read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 bit 76543210 0x29 (0x49) ocr1b[15:8] ocr1bh 0x28 (0x48) ocr1b[7:0] ocr1bl read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 bit 76543210 0x25 (0x45) icr1[15:8] icr1h 0x24 (0x44) icr1[7:0] icr1l read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000
115 8246a?avr?11/09 attiny2313a/4313 the input capture is updated with the counter (tcnt1) value each time an event occurs on the icp1 pin (or optionally on the analog comparator output for timer/counter1). the input capture can be used for defining the counter top value. the input capture register is 16-bit in size. to ensure that both the high and low bytes are read simultaneously when the cpu accesses these regi sters, the access is performed using an 8-bit temporary high byte register (temp). this temporary register is shared by all the other 16-bit registers. see ?accessing 16-bit registers? on page 106. 12.11.8 timsk ? timer/counter interrupt mask register ? bit 7 ? toie1: timer/counter1, overflow interrupt enable when this bit is written to one, and the i-flag in the status register is set (interrupts globally enabled), the timer/counter1 overflow interrupt is enabled. the corresponding interrupt vector ( see ?interrupts? on page 47. ) is executed when the tov1 flag, located in tifr, is set. ? bit 6 ? ocie1a: timer/counter1, output compare a match interrupt enable when this bit is written to one, and the i-flag in the status register is set (interrupts globally enabled), the timer/counter1 output compare a match interrupt is enabled. the corresponding interrupt vector ( see ?interrupts? on page 47. ) is executed when the ocf1a flag, located in tifr, is set. ? bit 5 ? ocie1b: timer/counter1, output compare b match interrupt enable when this bit is written to one, and the i-flag in the status register is set (interrupts globally enabled), the timer/counter1 output compare b match interrupt is enabled. the corresponding interrupt vector ( see ?interrupts? on page 47. ) is executed when the ocf1b flag, located in tifr, is set. ? bit 3 ? icie1: timer/counter1, input capture interrupt enable when this bit is written to one, and the i-flag in the status register is set (interrupts globally enabled), the timer/counter1 input capture interrupt is enabled. the corresponding interrupt vector ( see ?interrupts? on page 47. ) is executed when the icf1 flag, located in tifr, is set. 12.11.9 tifr ? timer/counter interrupt flag register ? bit 7 ? tov1: timer/counter1, overflow flag the setting of this flag is dependent of the wg m13:0 bits setting. in normal and ctc modes, the tov1 flag is set when the timer overflows. refer to table 12-5 on page 112 for the tov1 flag behavior when using a nother wgm13:0 bit setting. tov1 is automatically cleared when the timer/c ounter1 overflow interrupt vector is executed. alternatively, tov1 can be cleared by writing a logic one to its bit location. bit 7 6 5 4 3 2 1 0 0x39 (0x59) toie1 ocie1a ocie1b ?icie1 ocie0b toie0 ocie0a timsk read/write r/w r/w r/w r r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 bit 76543210 0x38 (0x58) tov1 ocf1a ocf1b ? icf1 ocf0b tov0 ocf0a tifr read/write r/w r/w r/w r r/w r/w r/w r/w initial value00000000
116 8246a?avr?11/09 attiny2313a/4313 ? bit 6 ? ocf1a: timer/counter1, output compare a match flag this flag is set in the timer clock cycle afte r the counter (tcnt1) va lue matches the output compare register a (ocr1a). note that a forced output compare (foc 1a) strobe will not set the ocf1a flag. ocf1a is automatically cleared when the outp ut compare match a interrupt vector is exe- cuted. alternatively, ocf1a can be cleared by writing a logic one to its bit location. ? bit 5 ? ocf1b: timer/counter1, output compare b match flag this flag is set in the timer clock cycle afte r the counter (tcnt1) va lue matches the output compare register b (ocr1b). note that a forced output compare (foc 1b) strobe will not set the ocf1b flag. ocf1b is automatically cleared when the outp ut compare match b interrupt vector is exe- cuted. alternatively, ocf1b can be cleared by writing a logic one to its bit location. ? bit 3 ? icf1: timer/count er1, input capture flag this flag is set when a capture event occurs on the icp1 pin. when th e input capture register (icr1) is set by the wgm13:0 to be used as the to p value, the icf1 flag is set when the coun- ter reaches the top value. icf1 is automatically cleared when the input capt ure interrupt vector is executed. alternatively, icf1 can be cleared by writing a logic one to its bit location.
117 8246a?avr?11/09 attiny2313a/4313 13. timer/counter0 and ti mer/counter1 prescalers timer/counter1 and time r/counter0 share the same prescale r module, but the timer/counters can have different prescaler settings. the description below applies to both timer/counter1 and timer/counter0. 13.1 internal clock source the timer/counter can be clocked directly by the system clock (by setting the csn2:0 = 1). this provides the fastest operation, with a maximum timer/counter clock frequency equal to system clock frequency (f clk_i/o ). alternatively, one of four taps from the prescaler can be used as a clock source. the prescaled clock has a frequency of either f clk_i/o /8, f clk_i/o /64, f clk_i/o /256, or f clk_i/o /1024. 13.2 prescaler reset the prescaler is free running, i.e., operates independently of the clock select logic of the timer/counter, and it is shared by timer/counter1 and timer/counter0. since the prescaler is not affected by the timer/counter? s clock select, the state of t he prescaler will have implications for situations where a prescaled clock is used. one example of prescaling artifacts occurs when the timer is enabled and clocked by the prescaler (6 > csn2:0 > 1). the number of system clock cycles from when the timer is enabled to the first count occurs can be from 1 to n+1 system clock cycles, where n equals the prescaler divisor (8, 64, 256, or 1024). it is possible to use the prescaler reset for synchronizing the timer/counter to program execu- tion. however, care must be taken if the othe r timer/counter that shares the same prescaler also uses prescaling. a prescaler reset will affect the prescaler period for all timer/counters it is connected to. 13.3 external clock source an external clock source applied to the t1/t0 pin can be used as timer/counter clock (clk t1 /clk t0 ). the t1/t0 pin is sampled once every syst em clock cycle by the pin synchronization logic. the synchronized (sampled) signal is then passed through the edge detector. figure 13-1 shows a functional equivalent block diagram of the t1/t0 synchronization and edge detector logic. the registers are clocked at the po sitive edge of the internal system clock ( clk i/o ). the latch is transparent in the high period of the internal system clock. the edge detector generates one clk t1 /clk t 0 pulse for each positive (csn2:0 = 7) or negative (csn2:0 = 6) edge it detects. figure 13-1. t1/t0 pin sampling the synchronization and e dge detector logic introduces a de lay of 2.5 to 3.5 system clock cycles from an edge has been applied to the t1/t0 pin to the counter is updated. tn_sync (to clock select logic) edge detector synchronization dq dq le dq tn clk i/o
118 8246a?avr?11/09 attiny2313a/4313 enabling and disabling of the clock input must be done when t1/t0 has been stable for at least one system clock cycle, otherwise it is a risk t hat a false timer/counter clock pulse is generated. each half period of the exter nal clock applied must be longer than one system clock cycle to ensure correct sampling. the external clock must be guaranteed to have less than half the sys- tem clock frequency (f extclk < f clk_i/o /2) given a 50/50% duty cycle. since the edge detector uses sampling, the maximum frequency of an external clock it can detect is half the sampling fre- quency (nyquist sampling theorem). however, due to variation of the system clock frequency and duty cycle caused by oscillator source (crystal, resonator, and capacitors) tolerances, it is recommended that maximum frequency of an external clock source is less than f clk_i/o /2.5. an external clock source can not be prescaled. figure 13-2. prescaler for timer/counter0 and timer/counter1 (1) note: 1. the synchronization logic on the input pins ( t1/t0) is shown in figure 13-1 on page 117 . 13.4 register description 13.4.1 gtccr ? general timer/counter control register ? bits 7..1 ? res: reserved bits these bits are reserved bits in the atti ny2313a/4313 and will always read as zero. ? bit 0 ? psr10: prescaler reset timer/counter1 and timer/counter0 when this bit is one, timer/co unter1 and timer/counter 0 prescaler will be reset. this bit is nor- mally cleared immediately by hardware. note that timer/counter1 and timer/counter0 share the same prescaler and a reset of th is prescaler will affect both timers. psr10 clear clk t1 clk t0 t1 t0 clk i/o synchronization synchronization bit 7 6 5 4 3 2 1 0 0x23 (0x43) ? ? ? ? ? ? ?psr10gtccr read/write r r r r r r r r/w initial value 0 0 0 0 0 0 0 0
119 8246a?avr?11/09 attiny2313a/4313 14. usart 14.1 features ? full duplex operation (independent serial receive and transmit registers) ? asynchronous or synchronous operation ? master or slave clocked synchronous operation ? high resolution baud rate generator ? supports serial frames with 5, 6, 7, 8, or 9 data bits and 1 or 2 stop bits ? odd or even parity generation and parity check supported by hardware ? data overrun detection ? framing error detection ? noise filtering includes false start bit detection and digital low pass filter ? three separate interrupts on tx complete , tx data register empty and rx complete ? multi-processor communication mode ? double speed asynchronous communication mode 14.2 overview the universal synchronous and asynchronous serial receiver and transmitter (usart) is a highly flexible serial communication device. a simplified block diagram of the usart transmitter is shown in figure 14-1 . cpu accessible i/o registers and i/o pins are shown in bold. figure 14-1. usart block diagram (1) note: 1. refer to figure 1-1 on page 2 , table 10-9 on page 67 , and table 10-6 on page 65 for usart pin placement. parity generator ubrr[h:l] udr (transmit) ucsra ucsrb ucsrc baud rate generator transmit shift register receive shift register rxd txd pin control udr (receive) pin control xck data recovery clock recovery pin control tx control rx control parity checker data bus osc sync logic clock generator transmitter receiver
120 8246a?avr?11/09 attiny2313a/4313 the dashed boxes in the block diagram separate the three main parts of the usart (listed from the top): clock generator, transmitter and receiv er. control registers are shared by all units. the clock generation logic consis ts of synchronization logic fo r external clock input used by synchronous slave operation, and the baud rate generator. the xck (transfer clock) pin is only used by synchronous transfer mode. the transmitter consists of a single write buffer, a serial shift register, parity generator and control logic for handling different serial frame formats. the write buffer allows a continuous transfer of data without any delay between frames. the receiver is the most complex part of the usart module due to its clock and data recovery units. the recovery units are used for asynchronous data reception. in addition to the recovery units, the receiver includes a parity checker, control logic, a shift register and a two level receive buffer (udr). the receiver supports the same frame formats as the transmitter, and can detect frame error, data overrun and parity errors. 14.2.1 avr usart vs. avr uart ? compatibility the usart is fully compatible with the avr uart regarding: ? bit locations inside all usart registers. ? baud rate generation. ? transmitter operation. ? transmit buffer functionality. ? receiver operation. however, the receive buffering has two improvements that will a ffect the compat ibility in some special cases: ? a second buffer register has been added. th e two buffer registers operate as a circular fifo buffer. therefore the udr must only be read once for each incoming data! more important is the fact that th e error flags (fe and dor) and the ninth data bit (rxb8) are buffered with the data in the receive buffer. therefore the status bits must always be read before the udr register is read. otherwise the er ror status will be lost since the buffer state is lost. ? the receiver shift register can now act as a th ird buffer level. this is done by allowing the received data to remain in th e serial shift register (see figure 14-1 ) if the buffer registers are full, until a new start bit is detected. the usart is therefore more resistant to data overrun (dor) error conditions. the following control bits have changed name, but have same functionality and register location: ? chr9 is changed to ucsz2. ? or is changed to dor. 14.3 clock generation the clock generation logic generates the base clock for the transmitter and receiver. the usart supports four modes of clock operati on: normal asynchronous, double speed asyn- chronous, master synchronous and slave sy nchronous mode. the umsel bit in usart control and status register c (ucsrc) sele cts between asynchronous and synchronous oper- ation. double speed (asynchronous mode only) is controlled by the u2x found in the ucsra register. when using synchronous mode (umsel = 1), the data direction register for the xck pin (ddr_xck) controls whether the clock source is internal (master mode) or external (slave mode). the xck pin is only active when using synchronous mode. figure 14-2 shows a block diagram of the clock generation logic.
121 8246a?avr?11/09 attiny2313a/4313 figure 14-2. clock generation logic, block diagram signal description: txclk transmitter clock (internal signal). rxclk receiver base clock (internal signal). xcki input from xck pin (internal sign al). used for synchronous slave operation. xcko clock output to xck pin (internal si gnal). used for synchronous master operation. fosc xtal pin frequency (system clock). 14.3.1 internal clock generation ? the baud rate generator internal clock generation is used for the as ynchronous and the synchronous master modes of operation. the description in this section refers to figure 14-2 . the usart baud rate register (ubrr) and the down-counter connected to it function as a programmable prescaler or baud rate generator. the down-counter, running at system clock (f osc ), is loaded with the ubrr value each time the counter has counted down to zero or when the ubrrl register is written. a clock is gener ated each time the counter reaches zero. this clock is the baud rate ge nerator clock output (= f osc /(ubrr+1)). the transmitter divides the baud rate generator clock output by 2, 8 or 16 depending on mode. the baud rate generator out- put is used directly by the rece iver?s clock and data recovery un its. however, the recovery units use a state machine that uses 2, 8 or 16 stat es depending on mode set by the state of the umsel, u2x and ddr_xck bits. table 14-1 contains equations for calculating the baud rate (in bits per second) and for calculat- ing the ubrr value for each mode of operation using an internally generated clock source. prescaling down-counter /2 ubrr /4 /2 fosc ubrr+1 sync register osc xck pin txclk u2x umsel ddr_xck 0 1 0 1 xcki xcko ddr_xck rxcl k 0 1 1 0 edge detector ucpol
122 8246a?avr?11/09 attiny2313a/4313 note: 1. the baud rate is defined to be the transfer rate in bit per second (bps) baud baud rate (in bits per second, bps) f osc system oscillator clock frequency ubrr contents of the ubrrh and ubrrl registers, (0-4095) some examples of ubrr values for so me system clock fr equencies are found in table 14-9 (see page 141 ). 14.3.2 double speed operation (u2x) the transfer rate can be doubled by setting the u2x bit in ucsra. setting this bit only has effect for the asynchronous operation. set this bi t to zero when using synchronous operation. setting this bit will reduce the divisor of the baud rate divider from 16 to 8, effectively doubling the transfer rate for asynchronous communication. note however that the receiver will in this case only use half the number of samples (reduced from 16 to 8) for data sampling and clock recovery, and therefore a more accurate baud ra te setting and system clock are required when this mode is used. for the transmitter, there are no downsides. 14.3.3 external clock external clocking is used by the synchronous sl ave modes of operation. the description in this section refers to figure 14-2 for details. external clock input from the xck pin is sampled by a synchronization register to minimize the chance of meta-stability. the output from the synchronizati on register must then pass through an edge detector before it can be used by the transmitter and receiver. this process intro- duces a two cpu clock period delay and theref ore the maximum extern al xck clock frequency is limited by the following equation: note that f osc depends on the stability of the system clock source. it is therefore recommended to add some margin to avoid possible loss of data due to frequency variations. table 14-1. equations for calculating ba ud rate register setting operating mode equation for calculating baud rate (1) equation for calculating ubrr value asynchronous normal mode (u2x = 0) asynchronous double speed mode (u2x = 1) synchronous master mode baud f osc 16 ubrr 1 + () -------------------------------------- - = ubrr f osc 16 baud ----------------------- - 1 ? = baud f osc 8 ubrr 1 + () ----------------------------------- = ubrr f osc 8 baud -------------------- 1 ? = baud f osc 2 ubrr 1 + () ----------------------------------- = ubrr f osc 2 baud -------------------- 1 ? = f xck f osc 4 ----------- <
123 8246a?avr?11/09 attiny2313a/4313 14.3.4 synchronous clock operation when synchronous mode is used (umsel = 1), the xck pin will be used as either clock input (slave) or clock output (master). the depen dency between the clock edges and data sampling or data change is the same. the basic principle is that data input (on rxd) is sampled at the opposite xck clock edge of the edge the data output (txd) is changed. figure 14-3. synchronous mode xck timing. the ucpol bit ucrsc selects which xck clock edge is used for data sampling and which is used for data change. as figure 14-3 shows, when ucpol is zero the data will be changed at rising xck edge and sampled at falling xck edge. if ucpol is set, the data will be changed at falling xck edge and sampl ed at rising xck edge. 14.4 frame formats a serial frame is defined to be one character of da ta bits with synchronizat ion bits (start and stop bits), and optionally a pa rity bit for error checking. the u sart accepts all 30 combinations of the following as valid frame formats: ? 1 start bit ? 5, 6, 7, 8, or 9 data bits ? no, even or odd parity bit ? 1 or 2 stop bits a frame starts with the start bit followed by the le ast significant data bit. then the next data bits, up to a total of nine, are succeeding, ending with t he most significant bit. if enabled, the parity bit is inserted after the data bits, before the stop bits . when a complete frame is transmitted, it can be directly followed by a new frame, or the communication line can be set to an idle (high) state. figure 14-4 illustrates the possible combinations of th e frame formats. bits inside brackets are optional. figure 14-4. frame formats st start bit, always low. rxd / txd xck rxd / txd xck ucpol = 0 ucpol = 1 sample sample 1 0 2 3 4 [5] [6] [7] [8] [p] st sp1 [sp2] (st / idle) (idle) frame
124 8246a?avr?11/09 attiny2313a/4313 (n) data bits (0 to 8). p parity bit. can be odd or even. sp stop bit, always high. idle no transfers on the communication line (rxd or txd). an idle line must be high. the frame format used by the usart is set by the ucsz2:0, upm1:0 and usbs bits in ucsrb and ucsrc. the receiver and transmitter use the same setting. note that changing the setting of any of these bits will corrup t all ongoing communicati on for both the rece iver and transmitter. the usart character size (ucsz2:0) bits select the number of data bits in the frame. the usart parity mode (upm1:0) bits enable and se t the type of parity bit. the selection between one or two stop bits is done by the usart stop bit select (usbs) bit. the receiver ignores the second stop bit. an fe (frame error) will therefor e only be detected in the cases where the first stop bit is zero. 14.4.1 parity bit calculation the parity bit is calculated by do ing an exclusive-or of all the data bits. if odd parity is used, the result of the exclusive or is inverted. the re lation between the parity bit and data bits is as follows: p even parity bit using even parity p odd parity bit using odd parity d n data bit n of the character if used, the parity bit is locate d between the last data bit and first stop bit of a serial frame. 14.5 usart initialization the usart has to be initialized before any communi cation can take place. the initialization pro- cess normally consists of setting the baud rate, setting frame format and enabling the transmitter or the receiver depending on the usag e. for interrupt driven usart operation, the global interrupt flag should be cleared (and interrupts globally disabled) when doing the initialization. before doing a re-initialization with changed baud rate or frame format, be sure that there are no ongoing transmissions during the period the regist ers are changed. the txc flag can be used to check that the transmitter has completed all transfers, and t he rxc flag can be used to check that there are no unread data in the receive buffer. note that the txc flag must be cleared before each transmission (before udr is wr itten) if it is used for this purpose. p even d n 1 ? d 3 d 2 d 1 d 0 0 p odd d n 1 ? d 3 d 2 d 1 d 0 1 = =
125 8246a?avr?11/09 attiny2313a/4313 the following simple usart initialization code examples show one assembly and one c func- tion that are equal in functionality. the exampl es assume asynchronous operation using polling (no interrupts enabled) and a fixed frame format. the baud rate is given as a function parameter. for the assembly code, the baud rate parameter is assumed to be stored in the r17:r16 registers. note: 1. see ?code examples? on page 6. more advanced initialization rout ines can be made that include frame format as parameters, dis- able interrupts and so on. however, many appl ications use a fixed setting of the baud and control registers, and for these ty pes of applications the initializat ion code can be placed directly in the main routine, or be combined with initialization code for other i/o modules. 14.6 data transmission ? the usart transmitter the usart transmitter is enabled by setting the transmit enable (txen) bit in the ucsrb register. when the transmitter is enabled, the no rmal port operation of the txd pin is overrid- den by the usart and given the function as t he transmitter?s serial output. the baud rate, mode of operation and frame format must be set up once before doing any transmissions. if syn- chronous operation is used, the clock on the xck pin will be overridden and used as transmission clock. assembly code example (1) usart_init: ; set baud rate out ubrrh, r17 out ubrrl, r16 ; enable receiver and transmitter ldi r16, (1<>8); ubrrl = ( unsigned char )baud; /* enable receiver and transmitter */ ucsrb = (1< 126 8246a?avr?11/09 attiny2313a/4313 14.6.1 sending frames with 5 to 8 data bit a data transmission is initiated by loading the tr ansmit buffer with the data to be transmitted. the cpu can load the transmit buffer by writing to the udr i/o location. the buffered data in the transmit buffer will be moved to the shift register wh en the shift register is ready to send a new frame. the shift register is loaded with new data if it is in idle state (no ongoing transmission) or immediately after the last stop bit of the previous frame is transmitted. wh en the shift register is loaded with new data, it will transf er one complete frame at the ra te given by the baud register, u2x bit or by xck depending on mode of operation. the following code examples show a simple usart transmit function based on polling of the data register empty (udre) flag. when using frames with le ss than eight bits, the most signifi- cant bits written to the udr are ignored. the u sart has to be initialized before the function can be used. for the assembly code, the data to be sent is assumed to be stored in register r16 note: 1. see ?code examples? on page 6. the function simply waits for the transmit buffer to be empty by checking the udre flag, before loading it with new data to be tr ansmitted. if the data register empty interrupt is utilized, the interrupt routine writes t he data into the buffer. assembly code example (1) usart_transmit: ; wait for empty transmit buffer sbis ucsra,udre rjmp usart_transmit ; put data (r16) into buffer, sends the data out udr,r16 ret c code example (1) void usart_transmit( unsigned char data ) { /* wait for empty transmit buffer */ while ( !( ucsra & (1< 127 8246a?avr?11/09 attiny2313a/4313 14.6.2 sending frames with 9 data bit if 9-bit characters are used (ucsz = 7), the nint h bit must be written to the txb8 bit in ucsrb before the low byte of the character is written to udr. the following code examples show a transmit function that handles 9-bit characters. for the asse mbly code, the data to be sent is assumed to be stored in registers r17:r16. note: 1. see ?code examples? on page 6. 2. these transmit functions are written to be gener al functions. they can be optimized if the con- tents of the ucsrb is static. for example, onl y the txb8 bit of the ucsrb register is used after initialization. the ninth bit can be used for indicating an address frame when using multi processor communi- cation mode or for other protocol handling as for example synchronization. 14.6.3 transmitter flags and interrupts the usart transmitter has two flags that indi cate its state: usart data register empty (udre) and transmit complete (txc). both fl ags can be used for generating interrupts. the data register empty (udre) flag indicates w hether the transmit buffer is ready to receive new data. this bit is set when the transmit buffer is empty, and cleared when the transmit buffer assembly code example (1) usart_transmit: ; wait for empty transmit buffer sbis ucsra,udre rjmp usart_transmit ; copy 9th bit from r17 to txb8 cbi ucsrb,txb8 sbrc r17,0 sbi ucsrb,txb8 ; put lsb data (r16) into buffer, sends the data out udr,r16 ret c code example (1) void usart_transmit( unsigned int data ) { /* wait for empty transmit buffer */ while ( !( ucsra & (1< 128 8246a?avr?11/09 attiny2313a/4313 contains data to be transmitted that has not yet been moved into the shift register. for compat- ibility with future devices, alwa ys write this bit to zero w hen writing the ucsra register. when the data register empty interrupt enable (udrie) bit in ucsrb is written to one, the usart data register empty interrupt will be ex ecuted as long as udre is set (provided that global interrupts are enabled). udre is cleared by writing udr. when interrupt-driven data transmission is used, the data register empty interrupt routine must either write new data to udr in order to clear udre or disable the data register empty interrupt, otherwise a new inter- rupt will occur once the inte rrupt routine terminates. the transmit complete (txc) flag bit is set one when the entire frame in the transmit shift reg- ister has been shifted out and there are no new da ta currently present in the transmit buffer. the txc flag bit is automatically cleared when a transmit complete interrupt is executed, or it can be cleared by writing a one to its bit location. the txc flag is useful in half-duplex communication interfaces (like the rs-485 standard), where a transmitting application must enter receive mode and free the communication bus immedi ately after completing the transmission. when the transmit compete interrupt enable (t xcie) bit in ucsrb is set, the usart transmit complete interrupt will be executed when the txc flag becomes se t (provided that global inter- rupts are enabled). when the transmit complete interrupt is used, the interrupt handling routine does not have to clear the txc flag, this is done automatically when the interrupt is executed. 14.6.4 parity generator the parity generator calculates the parity bit for the serial frame data. when parity bit is enabled (upm1 = 1), the transmitter control logic inserts the parity bit between the last data bit and the first stop bit of the frame that is sent. 14.6.5 disabling the transmitter the disabling of the tran smitter (setting the txen to zero ) will not become effective until ongo- ing and pending transmissions are completed, i.e., when the transmit shift register and transmit buffer register do not contain data to be transmitted. when disabled, the transmitter will no longer override the txd pin.
129 8246a?avr?11/09 attiny2313a/4313 14.7 data reception ? the usart receiver the usart receiver is enabled by writing the receive enable (rxen) bit in the ucsrb regis- ter to one. when the receiver is enabled, the normal pin operation of the rxd pin is overridden by the usart and given the function as the re ceiver?s serial input. the baud rate, mode of operation and frame format must be set up once before any serial reception can be done. if syn- chronous operation is used, the clock on the xck pin will be used as transfer clock. 14.7.1 receiving frames with 5 to 8 data bits the receiver starts data reception when it detects a valid start bit. each bit that follows the start bit will be sampled at the ba ud rate or xck clock, and shifted into the receive shift register until the first stop bit of a frame is received. a seco nd stop bit will be ignored by the receiver. when the first stop bit is received, i.e., a complete seri al frame is present in the receive shift register, the contents of the sh ift register will be moved into the receive buffer. the receive buffer can then be read by reading the udr i/o location. the following code example shows a simple us art receive function based on polling of the receive complete (rxc) flag. when using frames with less than eight bi ts the most significant bits of the data read from t he udr will be masked to zero. t he usart has to be initialized before the function can be used. note: 1. see ?code examples? on page 6. the function simply waits for data to be present in the receive buffer by checking the rxc flag, before reading the buffer and returning the value. 14.7.2 receiving frames with 9 data bits if 9-bit characters are used (ucsz=7) the ninth bit must be read from the rxb8 bit in ucsrb before reading the low bits from th e udr. this rule applies to the fe, dor and upe status flags as well. read status from ucsra, then da ta from udr. reading the udr i/o location will assembly code example (1) usart_receive: ; wait for data to be received sbis ucsra, rxc rjmp usart_receive ; get and return received data from buffer in r16, udr ret c code example (1) unsigned char usart_receive( void ) { /* wait for data to be received */ while ( !(ucsra & (1< 130 8246a?avr?11/09 attiny2313a/4313 change the state of the receiv e buffer fifo and consequently the txb8, fe, dor and upe bits, which all are stored in the fifo, will change. the following code example shows a simple usart receive function that handles both nine bit characters and the status bits. note: 1. see ?code examples? on page 6. assembly code example (1) usart_receive: ; wait for data to be received sbis ucsra, rxc rjmp usart_receive ; get status and 9th bit, then data from buffer in r18, ucsra in r17, ucsrb in r16, udr ; if error, return -1 andi r18,(1<> 1) & 0x01; return ((resh << 8) | resl); }
131 8246a?avr?11/09 attiny2313a/4313 the receive function example reads all the i/o r egisters into the register file before any com- putation is done. this gives an optimal receive buffer utilization since the buffer location read will be free to accept new data as early as possible. 14.7.3 receive compete flag and interrupt the usart receiver has one flag th at indicates the receiver state. the receive complete (rxc) flag indicates if there are unread data present in the receive buf- fer. this flag is one when unread data exist in the receive buffer, and zero when the receive buffer is empty (i.e., does not contain any unread data). if the re ceiver is disabled (rxen = 0), the receive buffer will be flushed and consequently the rxc bit will become zero. when the receive complete inte rrupt enable (rxcie) in ucsrb is set, the usart receive complete interrupt will be executed as long as the rxc flag is set (provided that global inter- rupts are enabled). when interrupt-driven data reception is used, the receive complete routine must read the received data fr om udr in order to clear the rx c flag, otherwise a new interrupt will occur once the interr upt routine terminates. 14.7.4 receiver error flags the usart receiver has three error flags: fr ame error (fe), data overrun (dor) and parity error (upe). all can be accessed by reading ucsra. common for the error flags is that they are located in the receive buffer together with the fr ame for which they indica te the error status. due to the buffering of the error flags, the ucsr a must be read before the receive buffer (udr), since reading the udr i/o location changes the buffer read location. another equality for the error flags is that they can not be altered by so ftware doing a write to th e flag location. however, all flags must be set to zero when the ucsra is written for upward compatibility of future usart implementations. none of the error flags can generate interrupts. the frame error (fe) flag indicates the state of the first stop bit of the next readable frame stored in the receive buffer. the fe flag is zero when the stop bit was correctly read (as one), and the fe flag will be one when the stop bit wa s incorrect (zero). this flag can be used for detecting out-of-sync c onditions, detecting break conditions and protocol handling. the fe flag is not affected by the setting of the usbs bit in ucsrc since the re ceiver ignores all, except for the first, stop bits. for co mpatibility with future device s, always set this bit to zero when writing to ucsra. the data overrun (dor) flag indicates data loss due to a receiver buffer full condition. a data overrun occurs when the receive buf fer is full (two characters), it is a new character waiting in the receive shift register, and a new start bit is detected. if the dor flag is set there was one or more serial frame lost between the frame last read from udr, and the next frame read from udr. for compatibility with future devices, always write this bit to zero when writing to ucsra. the dor flag is cleared when the frame received was successfully moved from the shift regis- ter to the receive buffer. the parity error (upe) flag indicates that the ne xt frame in the receive buffer had a parity error when received. if parity check is not enabled the upe bit w ill always be read zero. for compati- bility with future devices, always set this bi t to zero when wr iting to ucsra. fo r more details see ?parity bit calculation? on page 124 and ?parity checker? on page 131 . 14.7.5 parity checker the parity checker is active when the high usar t parity mode (upm1) bit is set. type of parity check to be performed (odd or even) is selected by the upm0 bit. when enabled, the parity
132 8246a?avr?11/09 attiny2313a/4313 checker calculates the parity of the data bits in incoming frames and compares the result with the parity bit from the serial frame. the result of the check is stored in the receive buffer together with the received data and stop bits. the parity error (upe) flag can then be read by software to check if the frame had a parity error. the upe bit is set if the next character that can be read from the receive buffer had a parity error when received and the parity checking was enabled at that point (upm1 = 1). this bit is valid until the receive buffer (udr) is read. 14.7.6 disabling the receiver in contrast to the transmitter, disabling of the receiver will be immediate. data from ongoing receptions will therefore be lost. when disabled (i.e ., the rxen is set to zero) the receiver will no longer override the normal function of the rxd port pin. the receiver buffer fifo will be flushed when the receiver is disabled. remaining data in the buffer will be lost 14.7.7 flushing the receive buffer the receiver buffer fifo will be fl ushed when the receiver is disa bled, i.e., the buffer will be emptied of its contents. unread data will be los t. if the buffer has to be flushed during normal operation, due to for instance an error condition , read the udr i/o location until the rxc flag is cleared. the following code example shows how to flush the receive buffer. note: 1. see ?code examples? on page 6. 14.8 asynchronous data reception the usart includes a clock recovery and a data recovery unit for handling asynchronous data reception. the clock recovery l ogic is used for synchronizing the internally generated baud rate clock to the incoming asynchronous serial frames at the rxd pin. the data recovery logic sam- ples and low pass filters each incoming bit, ther eby improving the noise immunity of the receiver. the asynchronous reception operational range depends on the accuracy of the inter- nal baud rate clock, the rate of the incoming frames, and the frame size in number of bits. 14.8.1 asynchronous clock recovery the clock recovery logic synchronizes internal clock to the incoming serial frames. figure 14-5 illustrates the sampling process of th e start bit of an incoming frame. the sample rate is 16 times assembly code example (1) usart_flush: sbis ucsra, rxc ret in r16, udr rjmp usart_flush c code example (1) void usart_flush( void ) { unsigned char dummy; while ( ucsra & (1< 133 8246a?avr?11/09 attiny2313a/4313 the baud rate for normal mode, and eight times the baud rate for double speed mode. the hor- izontal arrows illustrate the sy nchronization variation due to t he sampling process. note the larger time variation when using the double speed mode (u2x = 1) of operation. samples denoted zero are samples done when the rxd li ne is idle (i.e., no communication activity). figure 14-5. start bit sampling when the clock recovery logic detects a high (idl e) to low (start) transi tion on the rxd line, the start bit detection sequence is initiated. let sample 1 denote the first zero-sample as shown in the figure. the clock recovery logic then uses samples 8, 9, and 10 for normal mode, and sam- ples 4, 5, and 6 for double speed mode (indicated with sample numbers inside boxes on the figure), to decide if a valid start bit is received. if two or more of these three samples have logical high levels (the majority wins), the start bit is rejected as a noise spike and the receiver starts looking for the next high to low-transition. if howe ver, a valid start bit is detected, the clock recov- ery logic is synchronized and the data recove ry can begin. the sy nchronization process is repeated for each start bit. 14.8.2 asynchronous data recovery when the receiver clock is synchronized to the start bit, the data recovery can begin. the data recovery unit uses a state machine that has 16 states for each bit in normal mode and eight states for each bit in double speed mode. figure 14-6 shows the sampling of the data bits and the parity bit. each of the samples is given a number that is equal to the state of the recovery unit. figure 14-6. sampling of data and parity bit the decision of the logic level of the received bit is taken by doing a majori ty voting of the logic value to the three sample s in the center of the received bi t. the center samples are emphasized on the figure by having the sample number insi de boxes. the majority voting process is done as follows: if two or all thre e samples have high levels, the received bit is registered to be a logic 1. if two or all three samples have low levels, the received bit is registered to be a logic 0. this majority voting process acts as a low pass filter for the incoming signal on the rxd pin. the recovery process is then repeated until a complete frame is received. including the first stop bit. note that the receiver only uses the first stop bit of a frame. 1234567 8 9 10 11 12 13 14 15 16 12 start idle 0 0 bit 0 3 123 4 5 678 12 0 rxd sample (u2x = 0) sample (u2x = 1) 1234567 8 9 10 11 12 13 14 15 16 1 bit n 123 4 5 678 1 rxd sample (u2x = 0) sample (u2x = 1)
134 8246a?avr?11/09 attiny2313a/4313 figure 14-7 shows the sampling of the stop bit and the ea rliest possible beginning of the start bit of the next frame. figure 14-7. stop bit sampling and ne xt start bit sampling the same majority voting is done to the stop bit as done for the other bits in the frame. if the stop bit is registered to have a logic 0 valu e, the frame error (f e) flag will be set. a new high to low transition indicating the start bit of a new frame can come right after the last of the bits used for majority vo ting. for normal speed mode, the first low level sample can be at point marked (a) in figure 14-7 . for double speed mode the firs t low level must be delayed to (b). (c) marks a stop bit of full length. the ear ly start bit detection influences the operational range of the receiver. 14.8.3 asynchronous operational range the operational range of the receiver is dependent on the mismatch between the received bit rate and the internally generated baud rate. if the transmitter is sending frames at too fast or too slow bit rates, or the internally generated baud rate of the receiver does not have a similar (see table 14-2 ) base frequency, the receiver will not be abl e to synchronize the frames to the start bit. the following equations can be used to calculate the ratio of the incoming data rate and internal receiver baud rate. d sum of character size and pari ty size (d = 5 to 10 bit) s samples per bit. s = 16 for normal speed mode and s = 8 for double speed mode. s f first sample number used for majority voting. s f = 8 for normal speed and s f = 4 for double speed mode. s m middle sample number used for majority voting. s m = 9 for normal speed and s m = 5 for double speed mode. r slow is the ratio of the slowest incoming data ra te that can be accepted in relation to the receiver baud rate. r fast is the ratio of the fastest in coming data rate that can be accepted in relation to the receiver baud rate. table 14-2 and table 14-3 list the maximum receiver baud rate error that can be tolerated. note that normal speed mode has higher toleration of baud rate variations. 1234567 8 9 10 0/1 0/1 0/1 stop 1 123 4 5 6 0/1 rxd sample (u2x = 0) sample (u2x = 1) (a) (b) (c) r slow d 1 + () s s 1 ? ds ? s f ++ ------------------------------------------ - = r fast d 2 + () s d 1 + () ss m + ----------------------------------- =
135 8246a?avr?11/09 attiny2313a/4313 the recommendations of the maximum receiver baud rate error was made under the assump- tion that the receiver and transmitter equally divides the maximum total error. there are two possible sources fo r the receivers baud rate erro r. the receiver?s system clock (xtal) will always have some minor instabilit y over the supply voltage range and the tempera- ture range. when using a crystal to generate the system clock, this is rarely a problem, but for a resonator the system clock may differ more than 2% depending of the resonators tolerance. the second source for the error is more controllable. the baud rate generator can not always do an exact division of the system frequen cy to get the baud rate wanted. in this case an ubrr value that gives an acceptable low error can be used if possible. 14.9 multi-processor communication mode setting the multi-processor communication mode (mpcm) bit in ucsra enables a filtering function of incoming frames received by the usart receiver. frames that do not contain address information will be ignored and not put into the receive buffer. this effectively reduces the number of incoming frames that has to be handled by the cpu, in a system with multiple mcus that communicate via the same serial bus. the transmitter is unaffected by the mpcm setting, but has to be used differently when it is a part of a system utilizing the multi-processor communication mode. if the receiver is set up to receive frames that c ontain 5 to 8 data bits, then the first stop bit indi- cates if the frame contains data or address information. if the receiver is set up for frames with nine data bits, then the ninth bit (rxb8) is used for identifying address and data frames. when table 14-2. recommended maximum receiver baud rate error for normal speed mode (u2x = 0) d # (data+parity bit) r slow (%) r fast (%) max total error (%) recommended max receiver error (%) 5 93.20 106.67 +6.67/-6.8 3.0 6 94.12 105.79 +5.79/-5.88 2.5 7 94.81 105.11 +5.11/-5.19 2.0 8 95.36 104.58 +4.58/-4.54 2.0 9 95.81 104.14 +4.14/-4.19 1.5 10 96.17 103.78 +3.78/-3.83 1.5 table 14-3. recommended maximum receiver baud rate error for double speed mode (u2x = 1) d # (data+parity bit) r slow (%) r fast (%) max total error (%) recommended max receiver error (%) 5 94.12 105.66 +5.66/-5.88 2.5 6 94.92 104.92 +4.92/-5.08 2.0 7 95.52 104,35 +4.35/-4.48 1.5 8 96.00 103.90 +3.90/-4.00 1.5 9 96.39 103.53 +3.53/-3.61 1.5 10 96.70 103.23 +3.23/-3.30 1.0
136 8246a?avr?11/09 attiny2313a/4313 the frame type bit (the first stop or the ninth bit) is one, the frame contains an address. when the frame type bit is zero the frame is a data frame. the multi-processor communication mode enables several slave mcus to receive data from a master mcu. this is done by first decoding an address frame to find out which mcu has been addressed. if a particular slave mcu has been addressed, it will receive the following data frames as normal, while the other slave mcus will ignore the received frames until another address frame is received. 14.9.1 using mpcm for an mcu to act as a master mcu, it can use a 9-bit character frame format (ucsz = 7). the ninth bit (txb8) must be set when an address frame (txb8 = 1) or cleared when a data frame (txb = 0) is being transmitted. the slave mcus must in this case be set to use a 9-bit character frame format. the following procedure should be used to exchange data in multi-processor communication mode: 1. all slave mcus are in multi-processor co mmunication mode (mpcm in ucsra is set). 2. the master mcu sends an address frame, and all slaves receive and read this frame. in the slave mcus, the rxc flag in ucsra will be set as normal. 3. each slave mcu reads the udr register and determines if it has been selected. if so, it clears the mpcm bit in ucsra, otherwis e it waits for the next address byte and keeps the mpcm setting. 4. the addressed mcu will receive all data fram es until a new address frame is received. the other slave mcus, which still have the mp cm bit set, will ignore the data frames. 5. when the last data frame is received by the addressed mcu, the addressed mcu sets the mpcm bit and waits for a new address frame from master. the process then repeats from 2. using any of the 5- to 8-bit character frame formats is possible, but impractical since the receiver must change between using n and n+ 1 character frame formats. this makes full- duplex operation difficult since t he transmitter and receiver uses the same character size set- ting. if 5- to 8-bit character frames are used, the transmitter must be set to use two stop bit (usbs = 1) since the first stop bit is used for indicating the frame type. do not use read-modify-write instructions (sbi and cbi) to set or clear the mpcm bit. the mpcm bit shares the same i/o location as the txc flag and this might accidentally be cleared when using sbi or cbi instructions. 14.10 register description 14.10.1 udr ? usart i/o data register the usart transmit data buffer register and usart receive data buffer registers share the same i/o address referred to as usart data register or udr. the transmit data buffer reg- ister (txb) will be the destination for data wri tten to the udr register location. reading the udr register location will return the contents of the receive data bu ffer register (rxb). bit 76543210 0x0c (0x2c) rxb[7:0] udr (read) 0x0c (0x2c) txb[7:0] udr (write) read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
137 8246a?avr?11/09 attiny2313a/4313 for 5-, 6-, or 7-bi t characters the upper unus ed bits will be ignored by the transmitter and set to zero by the receiver. the transmit buffer can only be written when the udre flag in the ucsra register is set. data written to udr when the udre flag is not set, will be ignored by the usart transmitter. when data is written to the tr ansmit buffer, and the transmitter is enabled, the transm itter will load the data into the transmit sh ift register when the shift register is empty. then the data will be seri- ally transmitted on the txd pin. the receive buffer consists of a two level fifo . the fifo will change its state whenever the receive buffer is accessed. due to this behavior of the receive buffer, do not use read-modify- write instructions (sbi and cbi) on this locati on. be careful when using bit test instructions (sbic and sbis), since these also will change the state of the fifo. 14.10.2 ucsra ? usart control and status register a ? bit 7 ? rxc: usart receive complete this flag bit is set when there are unread data in the receive buffer and cleared when the receive buffer is empty (i.e., does not contain any unread dat a). if the receiver is disabled, the receive buffer will be flushed and consequently the rx c bit will become zero. the rxc flag can be used to generate a receive complete interrup t (see description of the rxcie bit). ? bit 6 ? txc: usart transmit complete this flag bit is set when the en tire frame in the transmit shift register has been shifted out and there are no new data currently present in the transm it buffer (udr). the txc flag bit is auto- matically cleared when a transmit complete interrupt is executed, or it can be cleared by writing a one to its bit location. the txc flag can generate a transmit complete interrupt (see descrip- tion of the txcie bit). ? bit 5 ? udre: usart data register empty the udre flag indicates if the transmit buffer (u dr) is ready to receive new data. if udre is one, the buffer is empty, and therefore ready to be written. the udre flag can generate a data register empty interrupt (see description of the udrie bit). udre is set after a reset to indicate that the transmitter is ready. ? bit 4 ? fe: frame error this bit is set if the next character in the receive buffer had a frame error when received. i.e., when the first stop bit of the next character in th e receive buffer is zero. this bit is valid until the receive buffer (udr) is read. the fe bit is zero when the stop bit of received da ta is one. always set this bit to zero when writing to ucsra. ? bit 3 ? dor: data overrun this bit is set if a data overrun condition is detected. a data overrun occurs when the receive buffer is full (two characters), it is a new char acter waiting in the receive shift register, and a new start bit is detected. this bit is valid until the receive buffer (u dr) is read. always set this bit to zero when writing to ucsra. bit 76543210 0x0b (0x2b) rxc txc udre fe dor upe u2x mpcm ucsra read/write r r/w r r r r r/w r/w initial value00100000
138 8246a?avr?11/09 attiny2313a/4313 ? bit 2 ? upe: usart parity error this bit is set if the next character in the receive buffer had a parity error when received and the parity checking was enabled at that point (upm1 = 1). this bit is valid until the receive buffer (udr) is read. always set this bit to zero when writing to ucsra. ? bit 1 ? u2x: double the usart transmission speed this bit only has effect for the asynchronous operation. write this bit to zero when using syn- chronous operation. writing this bit to one will reduce the divisor of th e baud rate divider from 16 to 8 effectively dou- bling the transfer rate for asynchronous communication. ? bit 0 ? mpcm: multi-processor communication mode this bit enables the multi-processor communication mode. when the mpcm bit is written to one, all the incoming frames received by the usart receiver that do not contain address infor- mation will be ignored. the transmi tter is unaffected by the mpcm setting. for more detailed information see ?multi-processor communication mode? on page 135 . 14.10.3 ucsrb ? usart control and status register b ? bit 7 ? rxcie: rx complete interrupt enable writing this bit to one enables interrupt on the rxc flag. a usart receive complete interrupt will be generated only if the rxcie bi t is written to one, the global interrupt flag in sreg is writ- ten to one and the rxc bit in ucsra is set. ? bit 6 ? txcie: tx complete interrupt enable writing this bit to one enables interrupt on t he txc flag. a usart tran smit complete interrupt will be generated only if the txcie bit is written to one, the glo bal interrupt flag in sreg is writ- ten to one and the txc bit in ucsra is set. ? bit 5 ? udrie: usart data register empty interrupt enable writing this bit to one enables interrupt on the udre flag. a data register empty interrupt will be generated only if the udrie bit is written to one, the glob al interrupt flag in sreg is written to one and the udre bit in ucsra is set. ? bit 4 ? rxen: receiver enable writing this bit to one enables the usart receiv er. the receiver will override normal port oper- ation for the rxd pin when enabled. disabling the receiver will flush the receive buffer invalidating the fe, dor, and upe flags. ? bit 3 ? txen: transmitter enable writing this bit to one enable s the usart transmitter. the tr ansmitter will override normal port operation for the txd pin when enabled. the disab ling of the transmitter (writing txen to zero) will not become effective until ongo ing and pending transmissions ar e completed, i. e., when the transmit shift register and transmit buffer register do not contain data to be transmitted. when disabled, the transmitter will no longer override the txd port. bit 76543210 0x0a (0x2a) rxcie txcie udrie rxen txen ucsz2 rxb8 txb8 ucsrb read/write r/w r/w r/w r/w r/w r/w r r/w initial value00000000
139 8246a?avr?11/09 attiny2313a/4313 ? bit 2 ? ucsz2: character size the ucsz2 bits combined with the ucsz1:0 bit in ucsrc sets the number of data bits (char- acter size) in a frame the receiver and transmitter use. ? bit 1 ? rxb8: receive data bit 8 rxb8 is the ninth data bit of the received charac ter when operating with serial frames with nine data bits. must be read before reading the low bits from udr. ? bit 0 ? txb8: transmit data bit 8 txb8 is the ninth data bit in the character to be transmitted when operating with serial frames with nine data bits. must be writte n before writing the low bits to udr. 14.10.4 ucsrc ? usart control and status register c ? bits 7:6 ? umsel1:0: usart mode select these bits select the mode of oper ation of the usart as shown in table 14-4 . note: 1. for full description of the master spi mode (mspim) operation, see ?usart in spi mode? on page 145. ? bits 5:4 ? upm1:0: parity mode these bits enable and set type of parity generati on and check. if enabled, the transmitter will automatically generate and send t he parity of the transmitted data bits within each frame. the receiver will generate a parity va lue for the incoming data and co mpare it to th e upm0 setting. if a mismatch is detected, the upe flag in ucsra will be set. bit 7 6 543210 0x03 (0x23) umsel1 umsel0 upm1 upm0 usbs ucsz1 ucsz0 ucpol ucsrc read/write r r/w r/w r/w r/w r/w r/w r/w initial value0 0 000110 table 14-4. umsel bit settings umsel1 umsel0 mode 0 0 asynchronous usart 0 1 synchronous usart 10reserved 1 1 master spi (mspim) (1) table 14-5. upm bits settings upm1 upm0 parity mode 00disabled 01reserved 1 0 enabled, even parity 1 1 enabled, odd parity
140 8246a?avr?11/09 attiny2313a/4313 ? bit 3 ? usbs: stop bit select this bit selects the number of stop bits to be in serted by the transmitter. the receiver ignores this setting. ? bit 2:1 ? ucsz1:0: character size the ucsz1:0 bits combined with the ucsz2 bit in ucsrb sets the number of data bits (char- acter size) in a frame the receiver and transmitter use. see table 14-7 . ? bit 0 ? ucpol: clock polarity this bit is used for synchronous mode only. write this bit to zero when asynchronous mode is used. the ucpol bit sets the relationship betw een data output change and data input sample, and the synchronous clock (xck). 14.10.5 ubrrl and ubrrh ? usart baud rate registers table 14-6. usbs bit settings usbs stop bit(s) 01-bit 12-bit table 14-7. ucsz bits settings ucsz2 ucsz1 ucsz0 character size 0005-bit 0016-bit 0107-bit 0118-bit 100reserved 101reserved 110reserved 1119-bit table 14-8. ucpol bit settings ucpol transmitted data changed (output of txd pin) received data sampled (input on rxd pin) 0 rising xck edge falling xck edge 1 falling xck edge rising xck edge bit 151413121110 9 8 0x02 (0x22) ? ? ? ? ubrr[11:8] ubrrh 0x09 (0x29) ubrr[7:0] ubrrl bit 76543210 read/write r r r r r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 00000000
141 8246a?avr?11/09 attiny2313a/4313 ? bit 15:12 ? reserved bits these bits are reserved for future use. for com patibility with future devi ces, these bit must be written to zero when ubrrh is written. ? bit 11:0 ? ubrr11:0: usart baud rate register this is a 12-bit register which contains the usart baud rate. the ubrrh contains the four most significant bits, and the ubrrl contains th e eight least significant bits of the usart baud rate. ongoing transmissions by the transmitter and receiver will be corrupted if th e baud rate is changed. writing ubrrl will tr igger an immediate update of the baud rate prescaler. 14.11 examples of ba ud rate setting for standard crystal and resonator frequencies, the most commonly used baud rates for asyn- chronous operation can be generated by using t he ubrr settings, see the following tables from table 14-9 to table 14-12 . ubrr values which yield an actual baud rate differing less than 0.5% from the target baud rate, are bold in the table. higher error ratings are acceptable, but the receiver will have less noise resistance when the error ratings are high, especially for large serial frames (see ?asynchronous operational range? on page 134 ). the error values are calcu- lated using the following equation: error[%] baudrate closest match baudrate -------------------------------------------------------- 1 ? ?? ?? 100% ? = table 14-9. examples of ubrr settings for co mmonly used oscillator frequencies baud rate (bps) f osc = 1.0000 mhz f osc = 1.8432 mhz f osc = 2.0000 mhz u2x = 0 u2x = 1 u2x = 0 u2x = 1 u2x = 0 u2x = 1 ubrr error ubrr error ubrr error ubrr error ubrr error ubrr error 2400 25 0.2% 51 0.2% 47 0.0% 95 0.0% 51 0.2% 103 0.2% 4800 12 0.2% 25 0.2% 23 0.0% 47 0.0% 25 0.2% 51 0.2% 9600 6 -7.0% 12 0.2% 11 0.0% 23 0.0% 12 0.2% 25 0.2% 14.4k 3 8.5% 8 -3.5% 7 0.0% 15 0.0% 8 -3.5% 16 2.1% 19.2k 2 8.5% 6 -7.0% 5 0.0% 11 0.0% 6 -7.0% 12 0.2% 28.8k 1 8.5% 3 8.5% 3 0.0% 7 0.0% 3 8.5% 8 -3.5% 38.4k 1 -18.6% 2 8.5% 2 0.0% 5 0.0% 2 8.5% 6 -7.0% 57.6k 0 8.5% 1 8.5% 1 0.0% 3 0.0% 1 8.5% 3 8.5% 76.8k ? ? 1 -18.6% 1 -25.0% 2 0.0% 1 -18.6% 2 8.5% 115.2k ? ? 0 8.5% 0 0.0% 1 0.0% 0 8.5% 1 8.5% 230.4k??????00.0%???? 250k??????????00.0% max. (1) 62.5 kbps 125 kbps 115.2 kbps 230.4 kbps 125 kbps 250 kbps 1. ubrr = 0, error = 0.0%
142 8246a?avr?11/09 attiny2313a/4313 table 14-10. examples of ubrr settings for commonly used oscillator frequencies (continued) baud rate (bps) f osc = 3.6864 mhz f osc = 4.0000 mhz f osc = 7.3728 mhz u2x = 0 u2x = 1 u2x = 0 u2x = 1 u2x = 0 u2x = 1 ubrr error ubrr error ubrr error ubrr error ubrr error ubrr error 2400 95 0.0% 191 0.0% 103 0.2% 207 0.2% 191 0.0% 383 0.0% 4800 47 0.0% 95 0.0% 51 0.2% 103 0.2% 95 0.0% 191 0.0% 9600 23 0.0% 47 0.0% 25 0.2% 51 0.2% 47 0.0% 95 0.0% 14.4k 15 0.0% 31 0.0% 16 2.1% 34 -0.8% 31 0.0% 63 0.0% 19.2k 11 0.0% 23 0.0% 12 0. 2% 25 0.2% 23 0.0% 47 0.0% 28.8k 7 0.0% 15 0.0% 8 -3.5% 16 2.1% 15 0.0% 31 0.0% 38.4k 5 0.0% 11 0.0% 6 -7.0% 12 0.2% 11 0.0% 23 0.0% 57.6k 3 0.0% 7 0.0% 3 8.5% 8 -3.5% 7 0.0% 15 0.0% 76.8k 2 0.0% 5 0.0% 2 8.5% 6 -7.0% 5 0.0% 11 0.0% 115.2k 1 0.0% 3 0.0% 1 8.5% 3 8.5% 3 0.0% 7 0.0% 230.4k 0 0.0% 1 0.0% 0 8.5% 1 8.5% 1 0.0% 3 0.0% 250k 0 -7.8% 1 -7.8% 0 0.0% 1 0.0% 1 -7.8% 3 -7.8% 0.5m ? ? 0 -7.8% ? ? 0 0.0% 0 -7.8% 1 -7.8% 1m ??????????0-7.8% max. (1) 230.4 kbps 460.8 kbps 250 kbps 0.5 mbps 460.8 kbps 921.6 kbps 1. ubrr = 0, error = 0.0%
143 8246a?avr?11/09 attiny2313a/4313 table 14-11. examples of ubrr settings for commonly used oscillator frequencies (continued) baud rate (bps) f osc = 8.0000 mhz f osc = 11.0592 mhz f osc = 14.7456 mhz u2x = 0 u2x = 1 u2x = 0 u2x = 1 u2x = 0 u2x = 1 ubrr error ubrr error ubrr error ubrr error ubrr error ubrr error 2400 207 0.2% 416 -0.1% 287 0.0% 575 0.0% 383 0.0% 767 0.0% 4800 103 0.2% 207 0.2% 143 0.0% 287 0.0% 191 0.0% 383 0.0% 9600 51 0.2% 103 0.2% 71 0.0% 143 0.0% 95 0.0% 191 0.0% 14.4k 34 -0.8% 68 0.6% 47 0.0% 95 0.0% 63 0.0% 127 0.0% 19.2k 25 0.2% 51 0.2% 35 0. 0% 71 0.0% 47 0.0% 95 0.0% 28.8k 16 2.1% 34 -0.8% 23 0.0% 47 0.0% 31 0.0% 63 0.0% 38.4k 12 0.2% 25 0.2% 17 0. 0% 35 0.0% 23 0.0% 47 0.0% 57.6k 8 -3.5% 16 2.1% 11 0. 0% 23 0.0% 15 0.0% 31 0.0% 76.8k 6 -7.0% 12 0.2% 8 0.0% 17 0.0% 11 0.0% 23 0.0% 115.2k 3 8.5% 8 -3.5% 5 0.0% 11 0.0% 7 0.0% 15 0.0% 230.4k 1 8.5% 3 8.5% 2 0.0% 5 0.0% 3 0.0% 7 0.0% 250k 1 0.0% 3 0.0% 2 -7.8% 5 -7.8% 3 -7.8% 6 5.3% 0.5m 0 0.0% 1 0.0% ? ? 2 -7.8% 1 -7.8% 3 -7.8% 1m ? ? 0 0.0% ? ? ? ? 0 -7.8% 1 -7.8% max. (1) 0.5 mbps 1 mbps 691.2 kbps 1.3824 mbps 921.6 kbps 1.8432 mbps 1. ubrr = 0, error = 0.0%
144 8246a?avr?11/09 attiny2313a/4313 table 14-12. examples of ubrr settings for comm only used oscillator frequencies (continued) baud rate (bps) f osc = 16.0000 mhz u2x = 0 u2x = 1 ubrr error ubrr error 2400 416 -0.1% 832 0.0% 4800 207 0.2% 416 -0.1% 9600 103 0.2% 207 0.2% 14.4k 68 0.6% 138 -0.1% 19.2k 51 0.2% 103 0.2% 28.8k 34 -0.8% 68 0.6% 38.4k 25 0.2% 51 0.2% 57.6k 16 2.1% 34 -0.8% 76.8k 12 0.2% 25 0.2% 115.2k 8 -3.5% 16 2.1% 230.4k 3 8.5% 8 -3.5% 250k 3 0.0% 7 0.0% 0.5m 1 0.0% 3 0.0% 1m 0 0.0% 1 0.0% max. (1) 1. ubrr = 0, error = 0.0% 1 mbps 2 mbps
145 8246a?avr?11/09 attiny2313a/4313 15. usart in spi mode 15.1 features ? full duplex, three-wire synchronous data transfer ? master operation ? supports all four spi modes of operation (mode 0, 1, 2, and 3) ? lsb first or msb first data transfer (configurable data order) ? queued operation (double buffered) ? high resolution baud rate generator ? high speed operation (f xckmax = f ck /2) ? flexible interrupt generation 15.2 overview the universal synchronous and asynchronous seri al receiver and transmitter (usart) can be set to a master spi compliant mode of operation. setting both umsel1:0 bits to one enables the usar t in mspim logic. in this mode of opera- tion the spi master control logic takes direct control over the us art resources. these resources include the transmitter and receiver sh ift register and buffers, and the baud rate gen- erator. the parity generator and checker, the da ta and clock recovery logic, and the rx and tx control logic is disabled. the usart rx and tx control logic is replaced by a common spi transfer control logic. ho wever, the pin control logic and interr upt generation logi c is identical in both modes of operation. the i/o register locations are the same in both mo des. however, some of the functionality of the control registers changes when using mspim. 15.3 clock generation the clock generation logic generates the base clock for the transmitter and receiver. for usart mspim mode of operation only internal cl ock generation (i.e. mast er operation) is sup- ported. the data direction register for the xck pin (ddr_xck) must therefore be set to one (i.e. as output) for the usart in mspim to oper ate correctly. preferably the ddr_xck should be set up before the usart in mspim is enabled (i.e. txen and rxen bit set to one). the internal clock generation used in mspim mode is identical to the usart synchronous mas- ter mode. the baud rate or ubrr setting can t herefore be calculated us ing the same equations, see table 15-1 :
146 8246a?avr?11/09 attiny2313a/4313 note: 1. the baud rate is defined to be the transfer rate in bit per second (bps) baud baud rate (in bits per second, bps) f osc system oscillator clock frequency ubrr contents of the ubrrh and ubrrl registers, (0-4095) 15.4 spi data modes and timing there are four combinations of xck (sck) phase an d polarity with respect to serial data, which are determined by control bits ucpha and ucpol. the data tr ansfer timing diagrams are shown in figure 15-1 . data bits are shifted out and latched in on opposite edges of the xck sig- nal, ensuring sufficient time fo r data signals to stabilize. the ucpol and ucpha functionality is summarized in table 15-2 . note that changing the setting of any of these bits will corrupt all ongoing communication for both the receiver and transmitter. figure 15-1. ucpha and ucpol data transfer timing diagrams. table 15-1. equations for calculating ba ud rate register setting operating mode equation for calculating baud rate (1) equation for ca lculating ubrrn value synchronous master mode baud f osc 2 ubrr 1 + () ----------------------------------- = ubrr f osc 2 baud -------------------- 1 ? = table 15-2. ucpol and ucpha functionality- ucpol ucpha spi mode leading edge trailing edge 0 0 0 sample (rising) setup (falling) 0 1 1 setup (rising) sample (falling) 1 0 2 sample (falling) setup (rising) 1 1 3 setup (falling) sample (rising) xck data setup (txd) data sample (rxd) xck data setup (txd) data sample (rxd) xck data setup (txd) data sample (rxd) xck data setup (txd) data sample (rxd) ucpol=0 ucpol=1 ucpha=0 ucpha=1
147 8246a?avr?11/09 attiny2313a/4313 15.5 frame formats a serial frame for the mspim is defined to be one character of 8 data bits. the usart in mspim mode has two valid frame formats: ? 8-bit data with msb first ? 8-bit data with lsb first a frame starts with the least or most significant dat a bit. then the next data bits, up to a total of eight, are succeeding, ending with the most or least significant bit accordingly. when a complete frame is transmitted, a new frame can directly follow it, or the communication line can be set to an idle (high) state. the udord bit in ucsrc sets the frame form at used by the usart in mspim mode. the receiver and transmitter use the same setting. note that changing the setting of any of these bits will corrupt all ongoin g communication for both th e receiver and transmitter. 16-bit data transfer can be achieved by writing two data bytes to udr. a uart transmit com- plete interrupt will then signal that the 16-bit value ha s been shifted out. 15.5.1 usart mspim initialization the usart in mspim mode has to be initialized before any communication can take place. the initialization process normally cons ists of setting the baud rate, setting master mode of operation (by setting ddr_xck to one), setting frame format and enabling the transmitter and the receiver. only the transmitter can operate independently. for interrupt driven usart opera- tion, the global interrupt flag should be cleared (and thus interrupts globally disabled) when doing the initialization. note: to ensure immediate initializat ion of the xck output the baud-rate register (ubrr) must be zero at the time the transmitter is enabled. contra ry to the normal mode usart operation the ubrr must then be written to the desired value after t he transmitter is enabled, but before the first trans- mission is started. setting ubrr to zero before enabling the transmitter is not necessary if the initialization is done immediately after a reset since ubrr is reset to zero. before doing a re-initialization with changed baud rate, data mode, or frame format, be sure that there is no ongoing transmissions during the period the registers are changed. the txc flag can be used to check that the transmitter has completed all transfers, and the rxc flag can be used to check that there are no unread data in the receive buffer. note that the txc flag must be cleared before each transmission (before udr is written) if it is used for this purpose. the following simple usart initialization code examples show one assembly and one c func- tion that are equal in functionality. the examples assume polling (no interrupts enabled). the
148 8246a?avr?11/09 attiny2313a/4313 baud rate is given as a function parameter. for the assembly code, the baud rate parameter is assumed to be stored in the r17:r16 registers. note: 1. see ?code examples? on page 6. assembly code example (1) usart_init: clr r18 out ubrrh,r18 out ubrrl,r18 ; setting the xck port pin as output, enables master mode. sbi xck_ddr, xck ; set mspi mode of operation and spi data mode 0. ldi r18, (1< 149 8246a?avr?11/09 attiny2313a/4313 15.6 data transfer using the usart in mspi mode re quires the transmitter to be enabled, i.e. the txen bit in the ucsrb register is set to one. when the transmitte r is enabled, the normal port operation of the txd pin is overridden and given the function as the transmitter's serial output. enabling the receiver is optional and is done by setting the rxen bit in the ucsrb register to one. when the receiver is enabled, the normal pin operation of the rxd pin is overridden and given the function as the receiver's serial input. the xck will in both cases be used as the transfer clock. after initialization the usart is re ady for doing data transfers. a data transfer is initiated by writ- ing to the udr i/o location. this is the case for both sending and re ceiving data since the transmitter controls the transfer clock. the data written to udr is moved from the transmit buffer to the shift register when the shift re gister is ready to send a new frame. note: to keep the input buffer in sync with the number of data bytes transmitted, the udr register must be read once for each byte transmitted. the inpu t buffer operation is identical to normal usart mode, i.e. if an overflow occurs the character last received will be lost, not the first data in the buf- fer. this means that if four byte s are transferred, byte 1 first, then byte 2, 3, and 4, and the udr is not read before all transfers are completed, then byte 3 to be received will be lost, and not byte 1. the following code examples show a simple u sart in mspim mode transfer function based on polling of the data register empty (udre) flag and the receive complete (rxc) flag. the usart has to be initialized befor e the function can be used. for the assembly code, the data to be sent is assumed to be stor ed in register r16 and the data received will be available in the same register (r16) after the function returns. the function simply waits for the transmit buffer to be empty by checking the udre flag, before loading it with new data to be transmitted. t he function then waits for data to be present in the receive buffer by checking the rxc flag, befo re reading the buffer and returning the value.
150 8246a?avr?11/09 attiny2313a/4313 note: 1. see ?code examples? on page 6. 15.6.1 transmitter and receiver flags and interrupts the rxc, txc, and udre flags and corresponding interrupts in usart in mspim mode are identical in function to the normal usart operat ion. however, the rece iver error status flags (fe, dor, and pe) are not in use and is always read as zero. 15.6.2 disabling the tr ansmitter or receiver the disabling of the transmitter or receiver in usart in mspim mode is identical in function to the normal usart operation. assembly code example (1) usart_mspim_transfer: ; wait for empty transmit buffer sbis ucsra, udre rjmp usart_mspim_transfer ; put data (r16) into buffer, sends the data out udr,r16 ; wait for data to be received usart_mspim_wait_rxc: sbis ucsra, rxc rjmp usart_mspim_wait_rxc ; get and return received data from buffer in r16, udr ret c code example (1) unsigned char usart_receive( void ) { /* wait for empty transmit buffer */ while ( !( ucsra & (1< 151 8246a?avr?11/09 attiny2313a/4313 15.7 avr usart mspim vs. avr spi the usart in mspim mode is fully co mpatible with the avr spi regarding: ? master mode timing diagram. ? the ucpol bit functionality is identical to the spi cpol bit. ? the ucpha bit functionality is identical to the spi cpha bit. ? the udord bit functionality is identical to the spi dord bit. however, since the usart in mspim mode re uses the usart resources, the use of the usart in mspim mode is somewhat different comp ared to the spi. in addition to differences of the control register bits, and that only master operation is supported by the usart in mspim mode, the following features differ between the two modules: ? the usart in mspim mode includes (double) buffering of the transmitter. the spi has no buffer. ? the usart in mspim mode receiver includes an additional buffer level. ? the spi wcol (write collision) bit is not included in usart in mspim mode. ? the spi double speed mode (spi2x) bit is not included. however, the same effect is achieved by setting ubrrn accordingly. ? interrupt timing is not compatible. ? pin control differs due to the master on ly operation of the usart in mspim mode. a comparison of the usart in mspim mode and the spi pins is shown in table 15-3 on page 151 . table 15-3. comparison of usart in mspim mode and spi pins. usart_mspim spi comment txd mosi master out only rxd miso master in only xck sck (functionally identical) (n/a) ss not supported by usart in mspim
152 8246a?avr?11/09 attiny2313a/4313 15.8 register description the following section describes the registers used for spi operation using the usart. 15.8.1 udr ? usart mspim i/o data register the function and bit description of the usart data register (udr) in mspi mode is identical to normal usart operation. see ?udr ? u sart i/o data register? on page 136. 15.8.2 ucsra ? usart mspim cont rol and status register a ? bit 7 ? rxc: usart receive complete this flag bit is set when there are unread data in the receive buffer and cleared when the receive buffer is empty (i.e., does not contain any unread dat a). if the receiver is disabled, the receive buffer will be flushed and consequently the rxc bit will become zero. the rxc flag can be used to generate a receive complete inte rrupt (see descripti on of the rxcie bit). ? bit 6 ? txc: usart transmit complete this flag bit is set when the en tire frame in the transmit shift register has been shifted out and there are no new data currently present in the transmit buffer (udr). the txc flag bit is auto- matically cleared when a transmit complete interrupt is executed, or it can be cleared by writing a one to its bit location. the txc flag can gener ate a transmit complete interrupt (see descrip- tion of the txcie bit). ? bit 5 ? udre: usart data register empty the udre flag indicates if the transmit buffer (udr) is ready to receive new data. if udre is one, the buffer is empty, and th erefore ready to be written. the udre flag can generate a data register empty interrupt (see description of the u drie bit). udre is set after a reset to indicate that the transmitter is ready. ? bit 4:0 ? reserved bits in mspi mode when in mspi mode, these bits are reserved for future use. fo r compatibility with future devices, these bits must be written to zero when ucsra is written. 15.8.3 ucsrb ? usart mspim cont rol and status register b ? bit 7 ? rxcie: rx complete interrupt enable writing this bit to one enables interrupt on the rxc flag. a us art receive complete interrupt will be generated only if the rxcie bi t is written to one, the global interrupt flag in sreg is writ- ten to one and the rxc bit in ucsra is set. bit 7 6 5 4 3 2 1 0 0x0b (0x2b) rxc txc udre - - - - - ucsra read/write r r/w r r r r r r initial value 0 0 0 0 0 1 1 0 bit 7 6543210 0x0a (0x2a) rxcie txcie udrie rxen txen - - - ucsrb read/write r/w r/w r/w r/w r/w r r r initial value 0 0 0 0 0 1 1 0
153 8246a?avr?11/09 attiny2313a/4313 ? bit 6 ? txcie: tx complete interrupt enable writing this bit to one enables interrupt on the txc flag. a usar t transmit complete interrupt will be generated only if the txcie bit is written to one, the glo bal interrupt flag in sreg is writ- ten to one and the txc bit in ucsra is set. ? bit 5 ? udrie: usart data register empty interrupt enable writing this bit to one enables interrupt on the udre flag. a da ta register empty interrupt will be generated only if the udrie bit is written to one, the glob al interrupt flag in sreg is written to one and the udre bit in ucsra is set. ? bit 4 ? rxen: receiver enable writing this bit to one enable s the usart receiver in mspim mode. the receiver will override normal port operation for the rxd pin when enabl ed. disabling the receiver will flush the receive buffer. only enabling the receiver in mspi mode (i.e. setting rxen=1 and txen=0) has no meaning since it is the transmitter that controls the transfer clock and since only master mode is supported. ? bit 3 ? txen: transmitter enable writing this bit to one enable s the usart transmitter. the tr ansmitter will override normal port operation for the txd pin when enabled. the disab ling of the transmitter (writing txen to zero) will not become effective until ongo ing and pending transmissions ar e completed, i. e., when the transmit shift register and transmit buffer register do not contain data to be transmitted. when disabled, the transmitter will no longer override the txd port. ? bit 2:0 ? reserved bits in mspi mode when in mspi mode, these bits are reserved for future use. fo r compatibility with future devices, these bits must be written to zero when ucsrb is written. 15.8.4 ucsrc ? usart mspim cont rol and status register c ? bit 7:6 ? umsel1:0: usart mode select these bits select the mode of o peration of the usart as shown in table 15-4 . see ?ucsrc ? usart control and status register c? on page 139 for full description of the normal usart operation. the mspim is enabled when both um sel bits are set to one. the udord, ucpha, and ucpol can be set in the same write operation where the mspim is enabled. bit 7 6 5 4 3 2 1 0 0x03 (0x23) umsel1 umsel0 - - - udord ucpha ucpol ucsrc read/write r/w r/w r r r r/w r/w r/w initial value 0 0 0 0 0 1 1 0 table 15-4. umsel bits settings umsel1 umsel0 mode 0 0 asynchronous usart 0 1 synchronous usart 1 0 reserved 1 1 master spi (mspim)
154 8246a?avr?11/09 attiny2313a/4313 ? bit 5:3 ? reserved bits in mspi mode when in mspi mode, these bits are reserved for future use. fo r compatibility with future devices, these bits must be written to zero when ucsrc is written. ? bit 2 ? udord: data order when set to one the lsb of the data word is trans mitted first. when set to zero the msb of the data word is transmitted first. refer to th e frame formats section page 4 for details. ? bit 1 ? ucpha: clock phase the ucpha bit setting determine if data is sampled on the leasing edge (first) or tailing (last) edge of xck. refer to the spi data modes and timing section page 4 for details. ? bit 0 ? ucpol: clock polarity the ucpol bit sets the polarity of the xck cl ock. the combination of the ucpol and ucpha bit settings determine the timing of the data transfer. refer to the spi data modes and timing section page 4 for details. 15.8.5 ubrrl and ubrrh ? usart mspim baud rate registers the function and bit description of the baud rate registers in mspi mode is identical to normal usart operation. see ?ubrrl and ubrrh ? u sart baud rate registers? on page 140.
155 8246a?avr?11/09 attiny2313a/4313 16. usi ? universal serial interface 16.1 features ? two-wire synchronous data transfer (master or slave) ? three-wire synchronous data transfer (master or slave) ? data received interrupt ? wakeup from idle mode ? in two-wire mode: wake-up from all sleep modes, including power-down mode ? two-wire start condition detect or with interrupt capability 16.2 overview the universal serial interface (usi), provi des the basic hardware resources needed for serial communication. combined with a minimum of cont rol software, the usi allows significantly higher transfer rates and uses less code space t han solutions based on software only. interrupts are included to minimize the processor load. a simplified block diagram of the usi is shown in figure 16-1 for actual placement of i/o pins refer to ?pinout attiny2313a/4313? on page 2 . device-specific i/o register and bit locations are listed in the ?register description? on page 162 . figure 16-1. universal serial interface, block diagram the 8-bit usi data register (u sidr) contains the incoming a nd outgoing data. it is directly accessible via the data bus but a copy of the contents is also pl aced in the usi buffer register (usibr) where it can be retrieved later. if reading the usi data register directly, the register must be read as quickly as possible to ensure that no data is lost. the most significant bit of the usi data register is connected to one of two output pins (depend- ing on the mode configuration, see ?analog comparator? on page 167 ). there is a transparent latch between the output of the us i data register and the output pin, which delays the change data bus usipf usitc usiclk usics0 usics1 usioif usioie usidc usisif usiwm0 usiwm1 usisie bit7 two-wire clock control unit do (output only) di/sda (input/open drain) usck/scl (input/open drain) 4-bit counter usidr usisr dq le usicr clock hold tim0 comp bit0 [1] 3 0 1 2 3 0 1 2 0 1 2 usibr
156 8246a?avr?11/09 attiny2313a/4313 of data output to the opposite clock edge of the data input sampling. the serial input is always sampled from the data input (di) pin independent of the configuration. the 4-bit counter can be both read and written vi a the data bus, and it can generate an overflow interrupt. both the usi data register and the counter are clocked simultaneously by the same clock source. this allows the counter to count the number of bits received or transmitted and generate an interrupt when the transfer is comple te. note that when an external clock source is selected the counter counts both clock edges. this means the counter registers the number of clock edges and not the number of data bits. the clock can be selected from three different sources: the usck pin, timer/counter0 compare match or from software. the two-wire clock control unit can be configured to generate an interrupt when a start condition has been detected on the two-wire bus. it can also be set to generate wait states by holding the clock pin low after a start condition is detected, or after the counter overflows. 16.3 functional descriptions 16.3.1 three-wire mode the usi three-wire mode is compliant to the serial peripheral interface (spi) mode 0 and 1, but does not have the slave select (ss) pin functionality. however, this feature can be implemented in software if necessary. pin names used by this mode are: di, do, and usck. figure 16-2. three-wire mode operat ion, simplified diagram figure 16-2 shows two usi units operating in three-wir e mode, one as master and one as slave. the two usi data registers are interconnected in such way that after eight usck clocks, the data in each register has been interchanged. the same clock also increments the usi?s 4-bit counter. the counter overflow (interrupt) flag, or usioif, can therefore be used to determine when a transfer is completed. the clock is generated by the master device software by toggling the usck pin via the porta register or by writing a one to bit usitc bit in usicr. slave master bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 do di usck bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 do di usck portxn
157 8246a?avr?11/09 attiny2313a/4313 figure 16-3. three-wire mode, timing diagram the three-wire mode timing is shown in figure 16-3 at the top of the figure is a usck cycle ref- erence. one bit is shifted into the usi data register (usidr) for eac h of these cycles. the usck timing is shown for both external clock modes. in external clock mode 0 (usics0 = 0), di is sampled at positive edges, and do is changed (usi data regist er is shifted by one) at nega- tive edges. in external clock mode 1 (usics0 = 1) the opposite edges with respect to mode 0 are used. in other words, data is sampled at negative and output is changed at positive edges. the usi clock modes corresponds to the spi data mode 0 and 1. referring to the timing diagram ( figure 16-3 ), a bus transfer involves the following steps: 1. the slave and master devices set up their da ta outputs and, depending on the protocol used, enable their output drivers (mark a and b). the output is set up by writing the data to be transmitted to the usi data regi ster. the output is enabled by setting the corresponding bit in the data direction register of port a. note that there is not a pre- ferred order of points a and b in the figure, but both must be at least one half usck cycle before point c, where the data is sampled. this is in order to ensure that the data setup requirement is satisfied. th e 4-bit counter is reset to zero. 2. the master software generates a clock pulse by toggling the usck line twice (c and d). the bit values on the data input (di) pins are sampled by the usi on the first edge (c), and the data output is changed on the oppo site edge (d). the 4-bit counter will count both edges. 3. step 2. is repeated eight times for a complete register (byte) transfer. 4. after eight clock pulses (i.e., 16 clock edges) th e counter will overflow and indicate that the transfer has been completed. if usi buff er registers are not used the data bytes that have been transferred must now be proc essed before a new transfer can be initi- ated. the overflow in terrupt will wake up the processor if it is set to idle mode. depending on the protocol used the slav e device can now set its output to high impedance. 16.3.2 spi master operation example the following code demonstrates how to use the usi module as a spi master: spitransfer: out usidr,r16 ldi r16,(1< msb msb 654321lsb 1 2 3 4 5 6 7 8 654321lsb usck usck do di d c b a e cycle ( reference )
158 8246a?avr?11/09 attiny2313a/4313 spitransfer_loop: out usicr,r17 in r16, usisr sbrs r16, usioif rjmp spitransfer_loop in r16,usidr ret the code is size optimized using only eight in structions (plus return ). the code example assumes that the do and usck pins have been enabled as outputs in ddra. the value stored in register r16 prior to the function is called is transferred to the slave device, and when the transfer is completed the data received from t he slave is stored back into the register r16. the second and third instructions clear the us i counter overflow flag and the usi counter value. the fourth and fifth instructions set th ree-wire mode, positive edge clock, count at usitc strobe, and toggle usck. the loop is repeated 16 times. the following code demonstrates how to use the usi as an spi master with maximum speed (f sck = f ck /2): spitransfer_fast: out usidr,r16 ldi r16,(1< 159 8246a?avr?11/09 attiny2313a/4313 16.3.3 spi slave operation example the following code demonstrates how to use the usi module as a spi slave: init: ldi r16,(1< 160 8246a?avr?11/09 attiny2313a/4313 figure 16-4. two-wire mode operation, simplified diagram the data direction is not given by the physical layer. a protocol, like the one used by the twi- bus, must be implemented to control the data flow. figure 16-5. two-wire mode, typical timing diagram referring to the timing diagram ( figure 16-5 ), a bus transfer involves the following steps: 1. the start condition is generated by the master by forcing the sda low line while keep- ing the scl line high (a). sda can be forced low either by writing a zero to bit 7 of the usi data register, or by setting the corres ponding bit in the porta register to zero. note that the data direction register bi t must be set to one for the output to be enabled. the start detector logic of the slave device (see figure 16-6 on page 161 ) detects the start condition and sets the usisif flag. the flag can generate an interrupt if necessary. 2. in addition, the start detector will hold th e scl line low after the master has forced a negative edge on this line (b). this allows the slave to wake up from sleep or complete other tasks before setting up the usi data regist er to receive the address. this is done by clearing the start condition flag and resetting the counter. master slave bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 sda scl bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 two-wire clock control unit hold scl portxn sda scl vcc p s address 1 - 7 8 9 r/w ack ack 1 - 8 9 data ack 1 - 8 9 data sda scl a b d e c f
161 8246a?avr?11/09 attiny2313a/4313 3. the master set the first bit to be transfer red and releases the scl line (c). the slave samples the data and shifts it into the usi data register at the positive edge of the scl clock. 4. after eight bits containing slave address a nd data direction (read or write) have been transferred, the slave counter overflows and th e scl line is forced low (d). if the slave is not the one the master has addressed, it releases the scl line and waits for a new start condition. 5. when the slave is addressed, it holds the sda line low during the acknowledgment cycle before holding the scl line low again (i.e., the usi counter register must be set to 14 before releasing scl at (d)). depe nding on the r/w bit the master or slave enables its output. if the bit is set, a master read operation is in progress (i.e., the slave drives the sda line) the slave can hold the scl line low after the acknowledge (e). 6. multiple bytes can now be transmitted, all in same direction, unt il a stop condition is given by the master (f), or a new start condition is given. if the slave is not able to receive more data it does not acknowledge the data byte it has last received. when the master does a read operation it must terminate the operation by forcing the acknowledge bit low after th e last byte transmitted. 16.3.5 start condition detector the start condition detector is shown in figure 16-6 . the sda line is delayed (in the range of 50 to 300 ns) to ensure valid sampling of the scl lin e. the start condition detector is only enabled in two-wire mode. figure 16-6. start condition detector, logic diagram the start condition detector works asynchronous ly and can therefore wake up the processor from power-down sleep mode. however, the protocol used might have restrictions on the scl hold time. therefore, w hen using this feature the oscillator st art-up time (set by cksel fuses, see ?clock sources? on page 25 ) must also be taken into consideration. refer to the description of the usisif bit on page 164 for further details. 16.3.6 clock speed considerations maximum frequency for scl and sck is f ck / 2. this is also t he maximum data transmit and receive rate in both two- and three-wire mode . in two-wire slave mode the two-wire clock con- trol unit will hold the scl low unt il the slave is ready to receive more data. this may reduce the actual data rate in two-wire mode. sda scl write( usisif) clock hold usisif dq clr dq clr
162 8246a?avr?11/09 attiny2313a/4313 16.4 alternative usi usage the flexible design of the usi allows it to be used for other tasks when serial communication is not needed. below are some examples. 16.4.1 half-duplex asynchronous data transfer using the usi data register in three-wire mode it is possible to implement a more compact and higher performance uart than by software, only. 16.4.2 4-bit counter the 4-bit counter can be used as a stand-alone counter with overflow interrupt. note that if the counter is clocked externally, both cl ock edges will increment the counter value. 16.4.3 12-bit timer/counter combining the 4-bit usi counter with one of the 8-bit timer/counters creates a 12-bit counter. 16.4.4 edge triggered external interrupt by setting the counter to maximum value (f) it can function as an additional external interrupt. the overflow flag and interrupt enable bit are then used for the external interrupt. this feature is selected by the usics1 bit. 16.4.5 software interrupt the counter overflow interrupt can be used as a software interrupt trig gered by a clock strobe. 16.5 register description 16.5.1 usicr ? usi control register the usi control register includes bits for interrupt enable, sett ing the wire mode, selecting the clock and clock strobe. ? bit 7 ? usisie: start condition interrupt enable setting this bit to one enables the start condition de tector interrupt. if there is a pending interrupt and usisie and the global interrupt enable flag are set to one the interrupt will be executed immediately. refer to the usisif bit description on page 164 for further details. ? bit 6 ? usioie: counter overflow interrupt enable setting this bit to one enables the counter overfl ow interrupt. if there is a pending interrupt and usioie and the global interrup t enable flag are set to one th e interrupt will be executed imme- diately. refer to the us ioif bit description on page 165 for further details. ? bit 5:4 ? usiwm1:0: wire mode these bits set the type of wire mode to be used, as shown in table 16-1 on page 163 . basically, only the function of the outputs are affected by these bits. data and clock inputs are not affected by t he mode selected and will always have th e same function. the counter and usi bit 7654 3210 0x0d (0x2d) usisie usioie usiwm1 usiwm0 us ics1 usics0 usiclk usitc usicr read/write r/w r/w r/w r/w r/w r/w w w initial value 0 0 0 0 0 0 0 0
163 8246a?avr?11/09 attiny2313a/4313 data register can therefore be clocked externally and data input sampled, even when outputs are disabled. note: 1. the di and usck pins are renamed to serial data (sda) and serial clock (scl) respectively to avoid confusion between the modes of operation. ? bit 3:2 ? usics1:0: clock source select these bits set the clock source for the usi data register and counter. the data output latch ensures that the output is ch anged at the opposite edge of th e sampling of the data input (di/sda) when using external clock source (usck/scl). when software strobe or timer/counter0 compare match clock option is selected, the output latch is transparent and therefore the output is changed immediately. clearing the usics1:0 bits enables software strobe option. when using this option, writing a one to the usiclk bit clocks bot h the usi data register and t he counter. for external clock source (usics1 = 1), the usiclk bit is no longer used as a strobe, but selects between external clocking and software clocking by the usitc strobe bit. table 16-1. relationship between usiw m1:0 and usi operation usiwm1 usiwm0 description 00 outputs, clock hold, and start detector disabled. port pins operate as normal. 01 three-wire mode. uses do, di, and usck pins. the data output (do) pin overrides the corresponding bit in the porta register. however, the corresponding ddra bit still controls the data direction. when the port pin is set as input the pi n pull-up is controlled by the porta bit. the data input (di) and serial clock (usck) pins do not affect the normal port operation. when operating as master, cl ock pulses are software generated by toggling the porta register, while the da ta direction is set to output. the usitc bit in the usicr register can be used for this purpose. 10 two-wire mode. uses sda (di) and scl (usck) pins (1) . the serial data (sda) and the serial clock (scl) pins are bi-directional and use open-collector output drives. the outp ut drivers are enabled by setting the corresponding bit for sda and scl in the ddra register. when the output driver is enabled for the sda pin, the output driver will force the line sda low if the output of the usi data register or the corresponding bit in the porta register is zero. otherwise, the sda line will not be driven (i.e., it is released). when the scl pin output driver is enabled the scl line will be forced low if the corresponding bit in the po rta register is zero, or by the start detector. otherwise the scl line will not be driven. the scl line is held low when a start det ector detects a start condition and the output is enabled. clearing the start condi tion flag (usisif) releases the line. the sda and scl pin inputs is not affected by enabling this mode. pull-ups on the sda and scl port pin are disabled in two-wire mode. 11 two-wire mode. uses sda and scl pins. same operation as in two-wire mode above, except that the scl line is also held low when a counter overflow occurs , and until the counter overflow flag (usioif) is cleared.
164 8246a?avr?11/09 attiny2313a/4313 table 16-2 shows the relationship between the usic s1:0 and usiclk setting and clock source used for the usi data register and the 4-bit counter. ? bit 1 ? usiclk: clock strobe writing a one to this bit location strobes the usi data register to shift one step and the counter to increment by one, provided that the software clock strobe option has been selected by writing usics1:0 bits to zero. the out put will change immediately when the clock strobe is executed, i.e., during the same instruction cycle. the value shifted into the usi data register is sampled the previous instruction cycle. when an external clock source is selected (usics1 = 1), the us iclk function is changed from a clock strobe to a clock select register. setting the usiclk bit in this case will select the usitc strobe bit as clock sour ce for the 4-bit counter (see table 16-2 ). the bit will be read as zero. ? bit 0 ? usitc: toggle clock port pin writing a one to this bit location t oggles the usck/scl value either from 0 to 1, or from 1 to 0. the toggling is independent of the setting in the data direction register, but if the port value is to be shown on the pin the corresponding ddr pi n must be set as output (to one). this feature allows easy clock generation when implementing master devices. when an external clock source is selected (usics 1 = 1) and the usiclk bit is set to one, writ- ing to the usitc strobe bit will directly clock th e 4-bit counter. this allows an early detection of when the transfer is done when operating as a master device. the bit will read as zero. 16.5.2 usisr ? usi status register the status register contains interrupt flag s, line status flags and the counter value. ? bit 7 ? usisif: start condition interrupt flag when two-wire mode is selected, the usisif fl ag is set (to one) when a start condition has been detected. when three-wire mode or output disable mode has been selected any edge on the sck pin will set the flag. table 16-2. relationship betw een the usics1:0 and usiclk setting usics1 usics0 usiclk clock sour ce 4-bit counter clock source 0 0 0 no clock no clock 0 0 1 software clock strobe (usiclk) software clock strobe (usiclk) 0 1 x timer/counter0 compare matc h timer/counter0 compare match 1 0 0 external, positive edge external, both edges 1 1 0 external, negative edge external, both edges 1 0 1 external, positive edge software clock strobe (usitc) 1 1 1 external, negative edge software clock strobe (usitc) bit 7 6 5 4 3 2 1 0 0x0e (0x2e) usisif usioif usipf usidc usicnt3 usicnt2 usicnt1 usicnt0 usisr read/write r/w r/w r/w r r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
165 8246a?avr?11/09 attiny2313a/4313 if usisie bit in usicr and the global interrupt enable flag are set, an interrupt will be gener- ated when this flag is set. the flag will only be cl eared by writing a logical one to the usisif bit. clearing this bit will release the start detec tion hold of uscl in two-wire mode. a start condition interr upt will wakeup the processor from all sleep modes. ? bit 6 ? usioif: counter overflow interrupt flag this flag is set (one) when the 4-bit counter overfl ows (i.e., at the transition from 15 to 0). if the usioie bit in usicr and the glob al interrupt enable flag are se t an interrupt will also be gener- ated when the flag is set. the flag will only be cleared if a one is wr itten to the usioif bit. clearing this bit will release the counter overfl ow hold of scl in two-wire mode. a counter overflow interrupt will wakeup the processor from idle sleep mode. ? bit 5 ? usipf: stop condition flag when two-wire mode is selected, the usipf flag is set (one) when a stop condition has been detected. the flag is cleared by writing a one to this bit. note that this is not an interrupt flag. this signal is useful when implem enting two-wire bus master arbitration. ? bit 4 ? usidc: data output collision this bit is logical one when bit 7 in the usi data register differs from the physical pin value. the flag is only valid when two-wire mode is used. th is signal is useful when implementing two-wire bus master arbitration. ? bits 3:0 ? usicnt3:0: counter value these bits reflect the current 4-bit counter value. the 4-bit counter value can directly be read or written by the cpu. the 4-bit counter increments by one for each clock generated either by the external clock edge detector, by a timer/counter0 compare match, or by software using usiclk or usitc strobe bits. the clock source depends on the setting of the usics1:0 bits. for external clock operation a special feature is added that allows the clock to be generated by writing to the usitc strobe bit. this feature is enabled by choosing an external clock source (usics1 = 1) and writing a one to the usiclk bit. note that even when no wire mode is selected (usiwm1..0 = 0) the external clock input (usck/scl) can still be used by the counter. 16.5.3 usidr ? usi data register the usi data register can be accessed directly but a copy of the data can also be found in the usi buffer register. depending on the usics1:0 bits of the usi control register a (left) shift operation may be per- formed. the shift operation can be synchronised to an external clock ed ge, to a timer/counter0 compare match, or directly to software via the usiclk bit. if a serial clock occurs at the same cycle the register is written, the register will c ontain the value written a nd no shift is performed. bit 7 6 5 4 3 2 1 0 0x0f (0x2f) msb lsb usidr read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
166 8246a?avr?11/09 attiny2313a/4313 note that even when no wire mode is selected (usiwm1:0 = 0) both the external data input (di/sda) and the external clock input (usck/scl) can still be used by the usi data register. the output pin (do or sda, depending on the wire mode) is connected via the output latch to the most significant bit (bit 7) of the usi data register. the output latch ensures that data input is sampled and data output is changed on opposite clock edges. the latch is open (transparent) during the first half of a serial clock cycle when an external clock source is selected (usics1 = 1) and constantly open when an internal clock s ource is used (usics1 = 0). the output will be changed immediately when a new msb is written as long as the latch is open. note that the data direction register bit corres ponding to the output pin must be set to one in order to enable data output from the usi data register. 16.5.4 usibr ? usi buffer register instead of reading data from the usi data regi ster the usi buffer register can be used. this makes controlling the usi less time critical and gives the cpu more time to handle other pro- gram tasks. usi flags as set similarly as when reading the usidr register. the content of the usi data register is loaded to the usi buffer register when the transfer has been completed. bit 7 6 5 4 3 2 1 0 0x00 (0x20) msb lsb usibr read/write r r r r r r r r initial value 0 0 0 0 0 0 0 0
167 8246a?avr?11/09 attiny2313a/4313 17. analog comparator the analog comparator compares the input va lues on the positive pin ain0 and negative pin ain1. when the voltage on the positive pin ain0 is higher than the voltage on the negative pin ain1, the analog comparator output, aco, is set. the comparator?s output can be set to trigger the timer/counter1 input capture function. in addition, the comparator can trigger a separate interrupt, exclusive to the analog comparator. the user can select interrupt triggering on com- parator output rise, fall or toggle. a block diagram of the comparator and its surrounding logic is shown in figure 17-1 . figure 17-1. analog comparator block diagram 17.1 register description 17.1.1 acsr ? analog comparator control and status register ? bit 7 ? acd: analog comparator disable when this bit is written logic one, the power to the analog comparator is switched off. this bit can be set at any time to tu rn off the analog com parator. this will reduce power consumption in active and idle mode. when changing the acd bit, the analog comparator interrupt must be disabled by clearing the acie bit in acsr. ot herwise an interrupt can occur when the bit is changed. ? bit 6 ? acbg: analog comparator bandgap select when this bit is set, a fixed bandgap reference vo ltage replaces the positive input to the analog comparator. when this bit is clea red, ain0 is applied to the positive input of the analog compar- ator. when the bandgap reference is used as input to the analog comparator, it will take a certain time for the voltage to stabilize. if not stibilized, the fi rst conversion may give a wrong value. see ?internal voltage reference? on page 41. ? bit 5 ? aco: analog comparator output the output of the analog comparator is synchron ized and then directly connected to aco. the synchronization introduces a delay of 1 - 2 clock cycles. acbg bandgap reference bit 76543210 0x08 (0x28) acd acbg aco aci acie acic acis1 acis0 acsr read/write r/w r/w r r/w r/w r/w r/w r/w initial value00n/a00000
168 8246a?avr?11/09 attiny2313a/4313 ? bit 4 ? aci: analog comparator interrupt flag this bit is set by hardware when a comparator output event triggers t he interrupt mode defined by acis1 and acis0. the analog comparator interr upt routine is executed if the acie bit is set and the i-bit in sreg is set. aci is cleared by hardware when executing the corresponding inter- rupt handling vector. alternatively, aci is cleared by writing a logic one to the flag. ? bit 3 ? acie: analog comparator interrupt enable when the acie bit is written logic one and the i-bi t in the status register is set, the analog com- parator interrupt is activated. when writ ten logic zero, the interrupt is disabled. ? bit 2 ? acic: analog comparator input capture enable when written logic one, this bit enables the input ca pture function in timer/counter1 to be trig- gered by the analog comparator. the comparator outp ut is in this case directly connected to the input capture front-end logic, ma king the comparator utilize the noise canceler and edge select features of the timer/counter1 input capture interrupt. when written logic zero, no connection between the analog comparator and the input c apture function exists. to make the comparator trigger the timer/counter1 input capture interr upt, the icie1 bit in the timer interrupt mask register (timsk) must be set. ? bits 1, 0 ? acis1, acis0: analog comparator interrupt mode select these bits determine which compar ator events that trigger the an alog comparator interrupt. the different settings are shown in table 17-1 . when changing the acis1/acis0 bits, the analog comparator interrupt must be disabled by clearing its interrupt enable bit in the acsr re gister. otherwise an interrupt can occur when the bits are changed. 17.1.2 didr ? digital i nput disable register ? bit 1, 0 ? ain1d, ain0d: ai n1, ain0 digital input disable when this bit is written logic one, the digital inpu t buffer on the ain1/0 pin is disabled. the corre- sponding pin register bit will alwa ys read as zero when this bit is set. when an analog signal is applied to the ain1/0 pin and the digital input from this pin is not needed, this bit should be writ- ten logic one to reduce power consumption in the digital input buffer. table 17-1. acis1/acis0 settings acis1 acis0 interrupt mode 0 0 comparator interrupt on output toggle. 01reserved 1 0 comparator interrupt on falling output edge. 1 1 comparator interrupt on rising output edge. bit 76543210 0x01 (0x21) ? ? ? ? ? ? ain1d ain0d didr read/writerrrrrrr/wr/w initial value00000000
169 8246a?avr?11/09 attiny2313a/4313 18. debugwire on-ch ip debug system 18.1 features ? complete program flow control ? emulates all on-chip functions, both digital and analog, except reset pin ? real-time operation ? symbolic debugging support (both at c and assembler source level, or for other hlls) ? unlimited number of program break points (using software break points) ? non-intrusive operation ? electrical characte ristics identical to real device ? automatic configuration system ? high-speed operation ? programming of non-volatile memories 18.2 overview the debugwire on-chip debug system uses a one-wire, bi-directional interface to control the program flow, execute avr instructions in t he cpu and to program the different non-volatile memories. 18.3 physical interface when the debugwire enable (dwen) fuse is programmed and lock bits are unprogrammed, the debugwire system within the target device is activated. the reset port pin is configured as a wire-and (open-drain) bi-directional i/o pin with pull-up enabled and becomes the commu- nication gateway between target and emulator. figure 18-1 shows the schematic of a target mcu, with debugwire enabled, and the emulator connector. the system clock is not affected by debugwire and will always be the clock source selected by the cksel fuses. figure 18-1. the debugwire setup when designing a system where debugwire will be used, the following observations must be made for correct operation: d w gnd dw(reset) vcc 1.8 - 5.5 v
170 8246a?avr?11/09 attiny2313a/4313 ? pull-up resistor on the dw/(reset) line must be larger than 10k. however, the pull-up resistor is optional. ? connecting the reset pin directly to v cc will not work. ? capacitors inserted on the reset pin must be disconnected w hen using debugwire. ? all external reset sources must be disconnected. 18.4 software break points debugwire supports program memory break points by the avr break instruction. setting a break point in avr studio ? will insert a break instruction in the program memo ry. the instruc- tion replaced by the break instru ction will be stored. when program execution is continued, the stored instruction will be execut ed before continuing from the program memory. a break can be inserted manually by putting the break instruction in the program. the flash must be re-programmed each time a break point is changed. this is automatically handled by avr studio th rough the debugwire inte rface. the use of brea k points will therefore reduce the flash data retention. devices used for debugging purposes should not be shipped to end customers. 18.5 limitations of debugwire the debugwire communication pin (dw) is physica lly located on the same pin as external reset (reset). an external re set source is therefore not supported when the debugwire is enabled. the debugwire system accurately emulates all i/ o functions when running at full speed, i.e., when the program in the cpu is running. when the cpu is stopped, care must be taken while accessing some of the i/o registers via the de bugger (avr studio). see the debugwire docu- mentation for detailed descri ption of the limitations. the debugwire interface is asynchronous, whic h means that the debugger needs to synchro- nize to the system clock. if the system clock is changed by software (e.g . by writing clkps bits) communication via debugwire may fail. also, clock frequencies below 100 khz may cause communication problems. a programmed dwen fuse enable s some parts of the clock system to be running in all sleep modes. this will increase the power consumption while in sleep. thus, the dwen fuse should be disabled when debugwire is not used.
171 8246a?avr?11/09 attiny2313a/4313 18.6 register description the following section describes the registers used with the debugwire. 18.6.1 dwdr ? debugwire data register the dwdr register provides a communication channel from the running program in the mcu to the debugger. this register is only accessible by the debugwire and can therefore not be used as a general purpose register in the normal operations. bit 76543210 dwdr[7:0] dwdr read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000
172 8246a?avr?11/09 attiny2313a/4313 19. self-programming the flash the device provides a self-programming me chanism for downloading and uploading program code by the mcu itself. the self-programming ca n use any available data interface and associ- ated protocol to read code and write (progra m) that code into the program memory. the spm instruction is disabled by default but it ca n be enabled by programming the selfprgen fuse (to ?0?). the program memory is updated in a page by page fashion. before programming a page with the data stored in the temporary page buffer, the page must be erased. the temporary page buf- fer is filled one word at a time using spm and the buffer can be filled either before the page erase command or between a page erase and a page write operation: alternative 1, fill the bu ffer before a page erase ? fill temporary page buffer ? perform a page erase ? perform a page write alternative 2, fill the bu ffer after page erase ? perform a page erase ? fill temporary page buffer ? perform a page write if only a part of the page needs to be changed, th e rest of the page must be stored (for example in the temporary page buffer) before the erase, and then be re-written. when using alternative 1, the boot loader provides an effective read-mod ify-write feature which a llows the user software to first read the page, do the necessary changes, and then write back the modified data. if alter- native 2 is used, it is not possible to read the old data while loading since the page is already erased. the temporary page buffer can be accessed in a random sequence. it is essential that the page address used in both the page erase and page write operation is addressing the same page. 19.1 performing page erase by spm to execute page erase, set up the address in the z-pointer, write ?00000011? to spmcsr and execute spm within four clock cycl es after writing spmcsr. the data in r1 and r0 is ignored. the page address must be written to pcpage in the z-register. other bits in the z-pointer will be ignored during this operation. note: the cpu is halted duri ng the page erase operation. 19.2 filling the temporary buffer (page loading) to write an instruction word, set up the addres s in the z-pointer and data in r1:r0, write ?00000001? to spmcsr and execute spm within four clock cycles after writing spmcsr. the content of pcword in the z-register is used to address the data in the temporary buffer. the temporary buffer will auto-erase after a page write operation or by writing the ctpb bit in spmcsr. it is also erased after a system reset. no te that it is not possible to write more than one time to each address without erasing the temporary buffer. note: if the eeprom is written in the middle of an spm page load operation, all data loaded will be lost.
173 8246a?avr?11/09 attiny2313a/4313 19.3 performing a page write to execute page write, set up the address in the z-pointer, write ?00000101? to spmcsr and execute spm within four clock cycl es after writing spmcsr. the data in r1 and r0 is ignored. the page address must be written to pcpage. ot her bits in the z-point er must be written to zero during this operation. note: the cpu is halted during the page write operation. 19.4 addressing the flash during self-programming the z-pointer is used to address the spm commands. since the flash is organized in pages (see table 20-7 on page 181 ), the program counter can be treated as having two different sections. one sect ion, consisting of the least significant bits, is addressing the words within a page, while the most significant bits are addressing the pages. this is shown in figure 19-1 . note that the page erase and page write operations are addressed independently. therefore it is of major importance that the software addresses the same page in both the page erase and page write operation. the lpm instruction uses the z-pointer to store the address. since this instruction addresses the flash byte-by-byte, also the lsb (bit z0) of the z-pointer is used. figure 19-1. addressing the flash during spm (note:) note: the different variables used in figure 19-1 are listed in table 20-7 on page 181 . bit 151413121110 9 8 zh (r31) z15 z14 z13 z12 z11 z10 z9 z8 zl (r30) z7z6z5z4z3z2z1z0 76543210 program memory 0 1 15 z - register bit 0 zpagemsb word address within a page page address within the flash zpcmsb instruction word pag e pcword[pagemsb:0]: 00 01 02 pageend pag e pcword pcpage pcmsb pagemsb program counter
174 8246a?avr?11/09 attiny2313a/4313 19.5 eeprom write prevents writing to spmcsr note that an eeprom write oper ation will block all software progra mming to flash. reading the fuses and lock bits from software will also be prevented during t he eeprom write operation. it is recommended that the user checks the status bit (eepe) in the eecr register and verifies that the bit is cleared before writing to the spmcsr register. 19.6 reading lock bits, fuse bits an d signature data from software it is possible for firmware to read device fuse and lock bits. in addition, firmware can also read data from the device signature imprint table (see page 175 ). note: fuse and lock bits that are programmed, will be read as zero. fuse and lock bits that are unpro- grammed, will be read as one. 19.6.1 reading lock bits from firmware lock bit values are returned in the destination register after an lpm instruction has been issued within three cpu cycles after rflb and spmen bits have been set in spmcsr. the rflb and spmen bits automatically clear upon completion of reading the lock bits, or if no lpm instruction is executed within three cpu cycles, or if no spm instruction is executed within four cpu cycles. when rflb and spmen are cleared lpm functions normally. to read the lock bits, follow the below procedure: 1. load the z-pointer with 0x0001. 2. set rflb and spmen bits in spmcsr. 3. issue an lpm instruction within three clock cycles. 4. read the lock bits from the lpm destination register. if successful, the contents of the destination register are as follows. see section ?program and data memory lock bits? on page 178 for more information. 19.6.2 reading fuse bits from firmware the algorithm for reading fuse byte s is similar to the one described above for reading lock bits, only the addresses are different. to read the fuse low byte (flb), follow the below procedure: 1. load the z-pointer with 0x0000. 2. set rflb and spmen bits in spmcsr. 3. issue an lpm instruction within three clock cycles. 4. read the flb from the lpm destination register. if successful, the contents of the destination register are as follows. refer to table 20-5 on page 180 for a detailed description and mapping of the fuse low byte. bit 76543210 rd ??????lb2lb1 bit 76543210 rd flb7 flb6 flb5 flb4 flb3 flb2 flb1 flb0
175 8246a?avr?11/09 attiny2313a/4313 to read the fuse high byte (fhb), simply repl ace the address in the z-pointer with 0x0003 and repeat the procedure above. if successful, the cont ents of the destination re gister are as follows. refer to table 20-4 on page 179 for detailed description and mapping of the fuse high byte. to read the fuse extended byte (feb), replace the address in the z-pointer with 0x0002 and repeat the previous procedure. if successful, t he contents of the destination register are as follows. refer to table 20-3 on page 179 for detailed description and mapping of the fuse extended byte. 19.6.3 reading device signature imprint table from firmware to read the contents of th e device signature imprint table, follow the below procedure: 1. load the z-pointer with the table index. 2. set rsig and spmen bits in spmcsr. 3. issue an lpm instruction within three clock cycles. 4. wait three clock cycles for spmen bits to be cleared. 5. read table data from the lpm destination register. the rsig and spmen bits will auto-clear afte r three cpu cycles. when rsig and spmen are cleared, lpm will work as described in the ?avr instruction set? description. see program example below. note: see ?code examples? on page 6 . bit 76543210 rd fhb7 fhb6 fhb5 fhb4 fhb3 fhb2 fhb1 fhb0 bit 76543210 rd feb7 feb6 feb5 feb4 feb3 feb2 feb1 feb0 assembly code example dsit_read: ; uses z-pointer as table index ldi zh, 0 ldi zl, 1 ; preload spmcsr bits into r16, then write to spmcsr ldi r16, (1< 176 8246a?avr?11/09 attiny2313a/4313 19.7 preventing flash corruption during periods of low v cc , the flash program can be corrupted because the supply voltage is too low for the cpu and the flash to operate properly. these issues are the same as for board level systems using the flash, and the same design solutions should be applied. a flash program corruption can be caused by two situ ations when the voltage is too low. first, a regular write sequence to the flash requires a minimum voltage to operate correctly. secondly, the cpu itself can execute instruct ions incorrectly, if the supply voltage for executing instructions is too low. flash corruption can easily be avoided by following these design recommendations (one is sufficient): 1. keep the avr reset active (low) during peri ods of insufficient po wer supply voltage. this can be done by enabling the internal brown-out detector (bod) if the operating voltage matches the detection level. if not, an external low v cc reset protection circuit can be used. if a reset occurs while a write operation is in progress, the write operation will be completed provided that the po wer supply voltage is sufficient. 2. keep the avr core in power-down sleep mode during periods of low v cc . this will pre- vent the cpu from attempting to decode and execute instructions, effectively protecting the spmcsr register and thus the flash from unintentional writes. 19.8 programming time for flash when using spm the calibrated rc oscillator is used to time flash accesses. table 19-1 shows the typical pro- gramming time for flash accesses from the cpu. note: 1. the min and max programming times is per individual operation. 19.9 register description 19.9.1 spmcsr ? store program memory control and status register the store program memory control and status regi ster contains the control bits needed to con- trol the program memory operations. ? bits 7, 6 ? res: reserved bits these bits are reserved bits in the at tiny2313a/4313 and always read as zero. ? bit 5 ? rsig: read device signature imprint table issuing an lpm inst ruction within three cycles after rsig and spmen bits have been set in spmcsr will return the selected dat a (depending on z-pointer valu e) from the device signature table 19-1. spm programming time (1) symbol min programming time max programming time flash write (page erase, page write, and write lock bits by spm) 3.7 ms 4.5 ms bit 76543210 0x37 (0x57) ? ? rsig ctpb rflb pgwrt pgers spmen spmcsr read/write r r r/w r/w r/w r/w r/w r/w initial value00000000
177 8246a?avr?11/09 attiny2313a/4313 imprint table into the de stination register. see ?device signature imprint table? on page 180 for details. ? bit 4 ? ctpb: clear temporary page buffer if the ctpb bit is writte n while filling the temporary page bu ffer, the temporary page buffer will be cleared and the da ta will be lost. ? bit 3 ? rflb: read fuse and lock bits an lpm instruction within three cycles after rf lb and spmen are set in the spmcsr register, will read either the lock bits or t he fuse bits (depending on z0 in the z-pointer) in to the destina- tion register. see ?eeprom write preven ts writing to spmcsr? on page 174 for details. ? bit 2 ? pgwrt: page write if this bit is written to one at the same time as spmen, the next spm inst ruction within four clock cycles executes page write, with the data stored in the temporary buffer. the page address is taken from the high part of the z-pointer. the data in r1 and r0 are ignored. the pgwrt bit will auto-clear upon co mpletion of a page write, or if no spm instruction is ex ecuted within four clock cycles. the cpu is halted during the entire page write operation. ? bit 1 ? pgers: page erase if this bit is written to one at the same time as spmen, the next spm inst ruction within four clock cycles executes page erase. the page address is taken from the high part of the z-pointer. the data in r1 and r0 are ignored. the pgers bi t will auto-clear upon comp letion of a page erase, or if no spm instruction is executed within four clock cycles. the cpu is halted during the entire page write operation. ? bit 0 ? spmen: self programming enable this bit enables the spm instruction for the next fo ur clock cycles. if written to one together with either rsig, ctpb, rflb, pgwrt, or pgers, t he following spm instruct ion will have a spe- cial meaning, see description abo ve. if only spmen is written, the following spm instruction will store the value in r1:r0 in the temporary page buffer addressed by the z-pointer. the lsb of the z-pointer is ignored. the spmen bit will aut o-clear upon completion of an spm instruction, or if no spm instruction is executed within fo ur clock cycles. during page erase and page write, the spmen bit remains high until the operation is completed. writing any other combination than ?100001?, ?010001?, ?001001?, ?000101?, ?000011? or ?000001? in the lower six bits will have no effect.
178 8246a?avr?11/09 attiny2313a/4313 20. memory programming this section describes the different methods for programming attiny2313a/4313 memories. 20.1 program and data memory lock bits the attiny2313a/4313 provides tw o lock bits which can be left unprogrammed (?1?) or can be programmed (?0?) to obtain the additional features listed in table 20-2 . the lock bits can only be erased to ?1? with the chip erase command. program memory can be read out via the debug wire interface when the dwen fuse is pro- grammed, even if lock bits are set. thus, when lock bit security is required, debugwire should always be disabled by clearing the dwen fuse. note: 1. ?1? means unprogrammed, ?0? means programmed notes: 1. program the fuse bits and boot lock bits before programming the lb1 and lb2. 2. ?1? means unprogrammed, ?0? means programmed table 20-1. lock bit byte (1) lock bit byte bit no description default value 7 ? 1 (unprogrammed) 6 ? 1 (unprogrammed) 5 ? 1 (unprogrammed) 4 ? 1 (unprogrammed) 3 ? 1 (unprogrammed) 2 ? 1 (unprogrammed) lb2 1 lock bit 1 (unprogrammed) lb1 0 lock bit 1 (unprogrammed) table 20-2. lock bit protection modes memory lock bits (1)(2) protection type lb mode lb2 lb1 1 1 1 no memory lock features enabled. 210 further programming of the flash and eeprom is disabled in parallel and serial programming mode. the fuse bits are locked in both serial and parallel programming mode. (1) 300 further programming and verification of the flash and eeprom is disabled in para llel and serial programming mode. the boot lock bits and fuse bits are locked in both serial and parallel programming mode. (1)
179 8246a?avr?11/09 attiny2313a/4313 20.2 fuse bits the attiny2313a/4313 has three fuse bytes. table 20-3 , table 20-4 and table 20-5 describe briefly the functionality of all the fuses and how t hey are mapped into the fuse bytes. note that the fuses are read as logical zero, ?0?, if they are programmed. note: 1. enables spm instruction. see ?self-programming the flash? on page 172 . notes: 1. the spien fuse is not accessible in serial programming mode. 2. see ?wdtcsr ? watchdog timer control and status register? on page 44 for details. 3. never ship a product with the dwen fuse prog rammed regardless of the setting of lock bits. a programmed dwen fuse enables some parts of the clock system to be running in all sleep modes. this may increase the power consumption. 4. see table 21-5 on page 198 for bodlevel fuse decoding. 5. see ?alternate functions of port a? on page 61 for description of rstdisbl fuse. table 20-3. fuse extended byte fuse extended byte bit no description default value 7 ? 1 (unprogrammed) 6 ? 1 (unprogrammed) 5 ? 1 (unprogrammed) 4 ? 1 (unprogrammed) 3 ? 1 (unprogrammed) 2 ? 1 (unprogrammed) 1 ? 1 (unprogrammed) selfprgen (1) 0 self programming enable 1 (unprogrammed) table 20-4. fuse high byte fuse high byte bit no description default value dwen (3) 7 debugwire enable 1 (unprogrammed) eesave 6 eeprom memory is preserved through the chip erase 1 (unprogrammed, eeprom not preserved) spien (1) 5 enable serial program and data downloading 0 (programmed, spi prog. enabled) wdton (2) 4 watchdog timer always on 1 (unprogrammed) bodlevel2 (4) 3 brown-out detector trigger level 1 (unprogrammed) bodlevel1 (4) 2 brown-out detector trigger level 1 (unprogrammed) bodlevel0 (4) 1 brown-out detector trigger level 1 (unprogrammed) rstdisbl (5) 0 external reset disable 1 (unprogrammed)
180 8246a?avr?11/09 attiny2313a/4313 notes: 1. the default value of sut1..0 results in maximum start-up time for the default clock source. see table 6-4 on page 27 for details. 2. the default setting of cksel3..0 results in internal rc oscillator @ 8 mhz. the status of the fuse bits is not affected by chip erase. note that the fuse bits are locked if lock bit1 (lb1) is programmed. program the fuse bits before programming the lock bits. fuse bits can also be read by device firmware. see section ?reading the fuse and lock bits? on page 189 . 20.2.1 latching of fuses the fuse values are latched when the device enters programming mode and changes of the fuse values will have no effect until the part leaves programming mode. this does not apply to the eesave fuse which will take effect once it is programmed. the fuse s are also latched on power-up in normal mode. 20.3 device signature imprint table the device signature imprint table is a dedicated read only memory area used for storing miscel- laneous device information, such as the device signature and osci llator calibration data. most of this memory segment is reserved for internal use, as outlined in table 20-6 . byte addresses are used when the device itse lf reads the data with the lpm command. external programming devices must use word addresses. table 20-5. fuse low byte fuse low byte bit no description default value ckdiv8 7 divide clock by 8 0 (programmed) ckout 6 output clock on ck out pin 1 (unprogrammed) sut1 5 select start-up time 1 (unprogrammed) (1) sut0 4 select start-up time 0 (programmed) (1) cksel3 3 select clock source 0 (programmed) (2) cksel2 2 select clock source 1 (unprogrammed) (2) cksel1 1 select clock source 0 (programmed) (2) cksel0 0 select clock source 0 (programmed) (2) table 20-6. contents of device signature imprint table. word address byte address high byte 0x00 0x00 signature byte 0 (1) 0x01 calibration data for internal 8 mhz oscillator (2) 0x01 0x02 signature byte 1 (1) 0x03 calibration data for internal 4 mhz oscillator (2) 0x02 0x04 signature byte 2 (1) 0x05 ... reserved for internal use
181 8246a?avr?11/09 attiny2313a/4313 notes: 1. see section ?signature bytes? for more information. 2. see section ?calibration by te? for more information. 20.3.1 calibration byte the signature area of the attiny 2313a/4313 contains two bytes of calibration data for the inter- nal oscillator. the calibration data in the high byte of address 0x00 is for use with the oscillator set to 8.0 mhz operation. during reset, this byte is automatically written into the osccal regis- ter to ensure correct fr equency of the oscillator. there is a separate calibration byte for the internal oscillator in 4.0 mhz mode of operation but this data is not loaded automatically. the hard ware always loads the 8.0 mhz calibration data during reset. to use separate calibration da ta for the oscillator in 4.0 mhz mode the osccal register must be updated by firmware. the calib ration data for 4.0 mhz operation is located in the high byte at address 0x 01 of the signature area. 20.3.2 signature bytes all atmel microcontrollers have a three-byte signature code which identifies the device. this code can be read in both serial and parallel mode, also when the device is locked. the three bytes reside in a separate address space. for the attiny2313a t he signature bytes are: 1. 0x000: 0x1e (indicates manufactured by atmel). 2. 0x001: 0x91 (indicates 2kb flash memory). 3. 0x002: 0x0a (indicates attiny23 13a device when 0x001 is 0x91). for the attiny4313 the signature bytes are: 1. 0x000: 0x1e (indicates manufactured by atmel). 2. 0x001: 0x92 (indicates 4kb flash memory). 3. 0x002: 0x0d (indicates attiny 4313 device when 0x001 is 0x92). 20.4 page size table 20-7. no. of words in a page and no. of pages in the flash device flash size page size pcword no. of pages pcpage pcmsb attiny2313a 1k word (2k bytes) 16 words pc[3:0] 64 pc[9:4] 9 attiny4313 2k words (4k bytes) 32 words pc[4:0] 64 pc[10:5] 10 table 20-8. no. of words in a page and no. of pages in the eeprom device eeprom size page size pcword no. of pages pcpage eeamsb attiny2313a 128 bytes 4 bytes eea[1:0] 32 eea[6:2] 6 attiny4313 256 bytes 4 bytes eea[1:0] 64 eea[7:2] 7
182 8246a?avr?11/09 attiny2313a/4313 20.5 parallel programming paramete rs, pin mapping, and commands this section describes how to parallel progr am and verify flash program memory, eeprom data memory, memory lock bits, and fuse bits in the attiny2313a/4313. pulses are assumed to be at least 250 ns unless otherwise noted. 20.5.1 signal names in this section, some pins of the attiny2313a/4313 are referenced by signal names describing their functionality during parallel programming, see figure 20-1 and table 20-9 on page 182 . pins not described in the following table are referenced by pin names. figure 20-1. parallel programming the xa1/xa0 pins determine the action executed when the xtal1 pin is given a positive pulse. the bit coding is shown in table 20-11 on page 183 . table 20-9. pin name mapping signal name in programming mode pin name i/o function rdy/bsy pd1 o 0: device is busy programming, 1: device is ready for new command. oe pd2 i output enable (active low). wr pd3 i write pulse (active low). bs1/pagel pd4 i byte select 1 (?0? selects low byte, ?1? selects high byte). program memory and eepr om data page load. xa0 pd5 i xtal action bit 0 xa1/bs2 pd6 i xtal action bit 1. byte select 2 (?0? selects low byte, ?1? selects 2?nd high byte). data i/o pb7-0 i/o bi-directiona l data bus (output when oe is low). vcc +5v gnd xtal1 pd1 pd2 pd3 pd4 pd5 pd6 pb7 - pb0 data i/o reset +12 v bs1/pagel xa0 xa1/bs2 oe rdy/bsy wr
183 8246a?avr?11/09 attiny2313a/4313 20.6 parallel programming 20.6.1 enter programming mode the following algorithm puts the device in parallel programming mode: 1. set prog_enable pins listed in table 20-10 on page 183 to ?0000?, reset pin and v cc to 0v. 2. apply 4.5 - 5.5v between v cc and gnd. 3. ensure that v cc reaches at least 1.8v within the next 20 s. table 20-10. pin values used to enter programming mode pin symbol value xa1 prog_enable[3] 0 xa0 prog_enable[2] 0 bs1 prog_enable[1] 0 wr prog_enable[0] 0 table 20-11. xa1 and xa0 coding xa1 xa0 action when xtal1 is pulsed 00 load flash or eeprom address (high or low address byte determined by bs1). 0 1 load data (high or low data byte for flash determined by bs1). 1 0 load command 1 1 no action, idle when pulsing wr or oe , the command loaded determines the action executed. the different commands are shown in table 20-12 . table 20-12. command byte bit coding command byte command executed 1000 0000 chip erase 0100 0000 write fuse bits 0010 0000 write lock bits 0001 0000 write flash 0001 0001 write eeprom 0000 1000 read signature bytes and calibration byte 0000 0100 read fuse and lock bits 0000 0010 read flash 0000 0011 read eeprom
184 8246a?avr?11/09 attiny2313a/4313 4. wait 20 - 60 s, and apply 11.5 - 12.5v to reset. 5. keep the prog_enable pins unchanged for at least 10s after the high-voltage has been applied to ensure the prog_enable signature has been latched. 6. wait at least 300 s before giving any parallel programming commands. 7. exit programming mode by po wer the device down or by bringing reset pin to 0v. if the rise time of the v cc is unable to fulfill the requiremen ts listed above, th e following alterna- tive algorithm can be used. 1. set prog_enable pins listed in table 20-10 on page 183 to ?0000?, reset pin to 0v and v cc to 0v. 2. apply 4.5 - 5.5v between v cc and gnd. 3. monitor v cc , and as soon as v cc reaches 0.9 - 1.1v, apply 11.5 - 12.5v to reset. 4. keep the prog_enable pins unchanged for at least 10s after the high-voltage has been applied to ensure the prog_enable signature has been latched. 5. wait until v cc actually reaches 4.5 -5.5v before giving any parallel programming commands. 6. exit programming mode by po wer the device down or by bringing reset pin to 0v. 20.6.2 considerations for efficient programming the loaded command and address are retained in the device during programming. for efficient programming, the following should be considered. ? the command needs only be loaded once when writing or reading multiple memory locations. ? skip writing the data value 0xff, that is the contents of the entire eeprom (unless the eesave fuse is programmed) a nd flash after a chip erase. ? address high byte needs only be loaded before programming or reading a new 256 word window in flash or 256 byte eeprom. this consideration also applies to signature bytes reading. 20.6.3 chip erase the chip erase will eras e the flash and eeprom (note:) memories plus lock bits. the lock bits are not reset until the program memory has been completely erased. the fuse bits are not changed. a chip erase must be perfor med before the flas h and/or eeprom are reprogrammed. note: the eeprpom memory is preserved during chip erase if the eesave fuse is programmed. load command ?chip erase? 1. set xa1, xa0 to ?10?. this enables command loading. 2. set bs1 to ?0?. 3. set data to ?1000 0000?. this is the command for chip erase. 4. give xtal1 a positive puls e. this loads the command. 5. give wr a negative pulse. this st arts the chip erase. rdy/bsy goes low. 6. wait until rdy/bsy goes high before loading a new command. 20.6.4 programming the flash the flash is organized in pages, see ?page size? on page 181 . when programming the flash, the program data is latched into a page buffer. this allows one page of program data to be pro-
185 8246a?avr?11/09 attiny2313a/4313 grammed simultaneously. the following procedure describes how to program the entire flash memory: a. load command ?write flash? 1. set xa1, xa0 to ?10?. this enables command loading. 2. set bs1 to ?0?. 3. set data to ?0001 0000?. this is the command for write flash. 4. give xtal1 a positive puls e. this loads the command. b. load address low byte 1. set xa1, xa0 to ?00?. this enables address loading. 2. set bs1 to ?0?. this selects low address. 3. set data = address low byte (0x00 - 0xff). 4. give xtal1 a positive pulse. this loads the address low byte. c. load data low byte 1. set xa1, xa0 to ?01?. this enables data loading. 2. set data = data low byte (0x00 - 0xff). 3. give xtal1 a positive pulse. this loads the data byte. d. load data high byte 1. set bs1 to ?1?. this selects high data byte. 2. set xa1, xa0 to ?01?. this enables data loading. 3. set data = data high byte (0x00 - 0xff). 4. give xtal1 a positive pulse. this loads the data byte. e. repeat b through e unt il the entire buffer is filled or unt il all data within the page is loaded. while the lower bits in the address are mapped to words within the page, the higher bits address the pages within the flash . this is illustrated in figure 20-2 on page 186 . note that if less than eight bits are required to address words in the page (pagesize < 256), the most significant bit(s) in the address low byte are used to address the page when performing a page write. f. load address high byte 1. set xa1, xa0 to ?00?. this enables address loading. 2. set bs1 to ?1?. this selects high address. 3. set data = address high byte (0x00 - 0xff). 4. give xtal1 a positive pulse. this loads the address high byte. g. program page 1. give wr a negative pulse. this starts programming of the entire page of data. rdy/bsy goes low. 2. wait until rdy/bsy goes high (see figure 20-3 on page 186 for signal waveforms). h. repeat b through h until the entire flash is programmed or until all data has been programmed. i. end page programming 1. 1. set xa1, xa0 to ?10?. this enables command loading. 2. set data to ?0000 0000?. this is the command for no operation.
186 8246a?avr?11/09 attiny2313a/4313 3. give xtal1 a positive pulse. this loads the command, and the internal write signals are reset. figure 20-2. addressing the flash which is organized in pages (1) note: 1. pcpage and pcword are listed in table 20-7 on page 181 . figure 20-3. programming the flash waveforms (1) note: 1. ?xx? is don?t care. the letters re fer to the programming description above. 20.6.5 programming the eeprom the eeprom is organized in pages, see table 20-8 on page 181 . when programming the eeprom, the program data is latche d into a page buffer. this al lows one page of data to be programmed simultaneously. th e programming algorithm for th e eeprom data memory is as follows (refer to ?programming the flash? on page 184 for details on command, address and data loading): 1. a: load command ?0001 0001?. 2. g: load address high byte (0x00 - 0xff). program memory word address within a page page address within the flash instruction word pag e pcword[pagemsb:0]: 00 01 02 pageend pag e pcword pcpage pcmsb pagemsb program counter rdy/bsy wr oe reset +12v pagel bs2 0x10 addr. low addr. high data data low data high addr. low data low data high xa1 xa0 bs1 xtal1 xx xx xx abcdeb cdegh f
187 8246a?avr?11/09 attiny2313a/4313 3. b: load address low byte (0x00 - 0xff). 4. c: load data (0x00 - 0xff). j: repeat 3 throug h 4 until the entire buffer is filled. k: program eeprom page 1. set bs1 to ?0?. 2. give wr a negative pulse. this starts pr ogramming of the eeprom page. rdy/bsy goes low. 3. wait until to rdy/bsy goes high before programming the next page (see figure 20-4 for signal waveforms). figure 20-4. programming the eeprom waveforms 20.6.6 reading the flash the algorithm for reading the flash memory is as follows (refer to ?programming the flash? on page 184 for details on command and address loading): 1. a: load command ?0000 0010?. 2. g: load address high byte (0x00 - 0xff). 3. b: load address low byte (0x00 - 0xff). 4. set oe to ?0?, and bs1 to ?0?. the flash word low byte can now be read at data. 5. set bs1 to ?1?. the flash word high byte can now be read at data. 6. set oe to ?1?. 20.6.7 reading the eeprom the algorithm for reading the eeprom memory is as follows (refer to ?programming the flash? on page 184 for details on command and address loading): 1. a: load command ?0000 0011?. 2. g: load address high byte (0x00 - 0xff). 3. b: load address low byte (0x00 - 0xff). 4. set oe to ?0?, and bs1 to ?0 ?. the eeprom data byte can now be read at data. 5. set oe to ?1?. rdy/bsy wr oe reset +12v pagel bs2 0x11 addr. high data addr. low data addr. low data xx xa1 xa0 bs1 xtal1 xx agbceb c el k
188 8246a?avr?11/09 attiny2313a/4313 20.6.8 programming the fuse low bits the algorithm for programming the fuse low bits is as follows (refer to ?programming the flash? on page 184 for details on command and data loading): 1. a: load command ?0100 0000?. 2. c: load data low byte. bit n = ?0? programs and bit n = ?1? erases the fuse bit. 3. give wr a negative pulse and wait for rdy/bsy to go high. 20.6.9 programming the fuse high bits the algorithm for programming the fuse high bits is as follows (refer to ?programming the flash? on page 184 for details on command and data loading): 1. a: load command ?0100 0000?. 2. c: load data low byte. bit n = ?0? programs and bit n = ?1? erases the fuse bit. 3. set bs1 to ?1? and bs2 to ?0?. this selects high data byte. 4. give wr a negative pulse and wait for rdy/bsy to go high. 5. set bs1 to ?0?. this selects low data byte. 20.6.10 programming the extended fuse bits the algorithm for programming the extended fuse bits is as follows (refer to ?programming the flash? on page 184 for details on command and data loading): 1. 1. a: load command ?0100 0000?. 2. 2. c: load data low byte. bit n = ?0? programs and bit n = ?1? erases the fuse bit. 3. 3. set bs1 to ?0? and bs2 to ?1?. this selects extended data byte. 4. 4. give wr a negative pulse and wait for rdy/bsy to go high. 5. 5. set bs2 to ?0?. this selects low data byte. figure 20-5. programming the fuses waveforms rdy/bsy wr oe reset +12v pagel 0x40 data data xx xa1 xa0 bs1 xtal1 ac 0x40 data xx ac write fuse low byte write fuse high byte 0x40 data xx ac write extended fuse byte bs2
189 8246a?avr?11/09 attiny2313a/4313 20.6.11 programming the lock bits the algorithm for programming the lock bits is as follows (refer to ?programming the flash? on page 184 for details on command and data loading): 1. a: load command ?0010 0000?. 2. c: load data low byte. bit n = ?0? programs the lock bit. if lb mode 3 is programmed (lb1 and lb2 is programmed), it is not possible to program the boot lock bits by any external programming mode. 3. give wr a negative pulse and wait for rdy/bsy to go high. the lock bits can only be cleared by executing chip erase. 20.6.12 reading the fuse and lock bits the algorithm for reading the fuse and lock bits is as follows (refer to ?programming the flash? on page 184 for details on command loading): 1. a: load command ?0000 0100?. 2. set oe to ?0?, bs2 to ?0? and bs1 to ?0?. the status of the fuse low bits can now be read at data (?0? means programmed). 3. set oe to ?0?, bs2 to ?1? and bs1 to ?1?. the status of the fuse high bits can now be read at data (?0? means programmed). 4. set oe to ?0?, bs2 to ?1?, and bs1 to ?0?. the status of the extended fuse bits can now be read at data (?0? means programmed). 5. set oe to ?0?, bs2 to ?0? and bs1 to ?1?. the status of the lock bits can now be read at data (?0? means programmed). 6. set oe to ?1?. figure 20-6. mapping between bs1, bs2 and the fuse and lock bits during read 20.6.13 reading the signature bytes the algorithm for reading the signatur e bytes is as follows (refer to ?programming the flash? on page 184 for details on command and address loading): 1. a: load command ?0000 1000?. 2. b: load address low byte (0x00 - 0x02). 3. set oe to ?0?, and bs1 to ?0?. the selected signature byte can now be read at data. 4. set oe to ?1?. lock bits 0 1 bs2 fuse high byte 0 1 bs1 data fuse low byte 0 1 bs2 extended fuse byte
190 8246a?avr?11/09 attiny2313a/4313 20.6.14 reading the calibration byte the algorithm for reading the calibrati on byte is as follows (refer to ?programming the flash? on page 184 for details on command and address loading): 1. a: load command ?0000 1000?. 2. b: load address low byte, 0x00. 3. set oe to ?0?, and bs1 to ?1?. the calibration byte can now be read at data. 4. set oe to ?1?. 20.7 serial downloading both the flash and eeprom memo ry arrays can be programmed us ing the serial spi bus while reset is pulled to gnd. the serial interface consists of pins sck, mosi (input) and miso (out- put). after reset is set low, the programming enable instruction needs to be executed first before program/erase operations can be executed. note, in table 20-13 on page 191 , the pin mapping for spi programming is listed. not all pa rts use the spi pins dedicated for the internal spi interface. figure 20-7. serial programming and verify (1) notes: 1. if the device is clocked by the internal oscillator, it is no nee d to connect a clock source to the xtal1 pin. 2. v cc - 0.3v < avcc < v cc + 0.3v, however, avcc should always be within 1.8 - 5.5v when programming the eeprom, an auto-erase cycle is built into the self-timed programming operation (in the serial mode only) and there is no need to first execute the chip erase instruction. the chip erase operation turns the content of every memory location in both the program and eeprom arrays into 0xff. depending on cksel fuses, a valid clock must be present. the minimum low and high periods for the serial clock (sck) input are defined as follows: low: > 2 cpu clock cycles for f ck < 12 mhz, 3 cpu clock cycles for f ck >= 12 mhz high: > 2 cpu clock cycles for f ck < 12 mhz, 3 cpu clock cycles for f ck >= 12 mhz vcc gnd xtal1 sck miso mosi reset +1.8 - 5.5v
191 8246a?avr?11/09 attiny2313a/4313 20.8 serial programming pin mapping 20.8.1 serial programming algorithm when writing serial data to t he attiny2313a/4313, data is clo cked on the rising edge of sck. when reading data from the at tiny2313a/4313, data is clocke d on the falling ed ge of sck. see figure 20.8.2 , figure 21-6 and table 21-8 for timing details. to program and verify the attiny2313a/4313 in the serial programming mode, the following sequence is recommended (see four byte instruction formats in table 20-15 on page 193 ): 1. power-up sequence: apply power between v cc and gnd while reset and sck are set to ?0?. in some sys- tems, the programmer can not guarantee that sck is held low during power-up. in this case, reset must be given a positive pulse of at least two cpu clock cycles duration after sck has been set to ?0?. 2. wait for at least 20 ms and enable serial programming by sending the programming enable serial instru ction to pin mosi. 3. the serial programming instructions will not work if the communication is out of syn- chronization. when in sync. the second byte (0x53), will ec ho back when issuing the third byte of the programming enable instructio n. whether the echo is correct or not, all four bytes of the instruction must be tran smitted. if the 0x53 did not echo back, give reset a positive pulse and issue a new programming enable command. 4. the flash is programmed one page at a time . the memory page is loaded one byte at a time by supplying the 4 lsb of the add ress and data together with the load program memory page instruction. to ensure correct loading of the page, the data low byte must be loaded before data high byte is appli ed for a given address. the program memory page is stored by loading the write program memory page instruction with the 6 msb of the address. if polling ( rdy/bsy ) is not used, the user must wait at least t wd_flash before issuing the next page. (see table 20-14 on page 192 .) accessing the serial pro- gramming interface before the flash write op eration completes can result in incorrect programming. 5. a: the eeprom array is progra mmed one byte at a time by supplying the address and data together with t he appropriate write in struction. an eeprom memory location is first automatically erased before new data is written. if polling (rdy/bsy ) is not used, the user must wait at least t wd_eeprom before issuing the next byte. (see table 20-14 on page 192 .) in a chip erased device, no 0xffs in the data file(s) need to be pro- grammed. b: the eeprom array is program med one page at a time. th e memory page is loaded one byte at a time by supplying the 2 lsb of the address and data together with the load eeprom memory page in struction. the eeprom memo ry page is stored by loading the write eeprom memory page inst ruction with the 5 msb of the address. when using eeprom page access only byte locations loaded with the load eeprom memory page instruction is altered. the remaining locations remain unchanged. if poll- ing ( rdy/bsy ) is not used, the used must wait at least t wd_eeprom before issuing the table 20-13. pin mapping serial programming symbol pins i/o description mosi pb5 i serial data in miso pb6 o serial data out sck pb7 i serial clock
192 8246a?avr?11/09 attiny2313a/4313 next page (see table 20-14 on page 192 ). in a chip erased device, no 0xff in the data file(s) need to be programmed. 6. any memory location can be verified by us ing the read instruct ion which returns the content at the selected addre ss at serial output miso. 7. at the end of the pr ogramming se ssion, reset can be set high to commence normal operation. 8. power-off sequence (if needed): set reset to ?1?. turn v cc power off. table 20-14. minimum wait delay before writing the next flash or eeprom location symbol minimum wait delay t wd_flash 4.5 ms t wd_eeprom 4.0 ms t wd_erase 9.0 ms t wd_fuse 4.5 ms
193 8246a?avr?11/09 attiny2313a/4313 20.8.2 serial programming instruction set table 20-15. serial programming instruction set instruction instruction format operation byte 1 byte 2 byte 3 byte4 programming enable 1010 1100 0101 0011 xxxx xxxx xxxx xxxx enable serial programming after reset goes low. chip erase 1010 1100 100x xxxx xxxx xxxx xxxx xxxx chip erase eeprom and flash. read program memory 0010 h 000 0000 00 aa bbbb bbbb oooo oooo read h (high or low) data o from program memory at word address a : b . load program memory page 0100 h 000 000x xxxx xxxx bbbb iiii iiii write h (high or low) data i to program memory page at word address b . data low byte must be loaded before data high byte is applied within the same address. write program memory page 0100 1100 0000 00 aa bbbb xxxx xxxx xxxx write program memory page at address a : b . read eeprom memory 1010 0000 000x xxxx x bbb bbbb oooo oooo read data o from eeprom memory at address b . write eeprom memory 1100 0000 000x xxxx x bbb bbbb iiii iiii write data i to eeprom memory at address b . load eeprom memory page (page access) 1100 0001 0000 0000 0000 00 bb iiii iiii load data i to eeprom memory page buffer. after data is loaded, program eeprom page. write eeprom memory page (page access) 1100 0010 00xx xxxx x bbb bb 00 xxxx xxxx write eeprom page at address b . read lock bits 0101 1000 0000 0000 xxxx xxxx xx oo oooo read lock bits. ?0? = programmed, ?1? = unprogrammed. see table 20-1 on page 178 for details. write lock bits 1010 1100 111x xxxx xxxx xxxx 11 ii iiii write lock bits. set bits = ?0? to program lock bits. see table 20-1 on page 178 for details. read signature byte 0011 0000 000x xxxx xxxx xx bb oooo oooo read signature byte o at address b . write fuse bits 1010 1100 1010 0000 xxxx xxxx iiii iiii set bits = ?0? to program, ?1? to unprogram. write fuse high bits 1010 1100 1010 1000 xxxx xxxx iiii iiii set bits = ?0? to program, ?1? to unprogram. write extended fuse bits 1010 1100 1010 0100 xxxx xxxx xxxx xxx i set bits = ?0? to program, ?1? to unprogram. read fuse bits 0101 0000 0000 0000 xxxx xxxx oooo oooo read fuse bits. ?0? = programmed, ?1? = unprogrammed. read fuse high bits 0101 1000 0000 1000 xxxx xxxx oooo oooo read fuse high bits. ?0? = pro- grammed, ?1? = unprogrammed.
194 8246a?avr?11/09 attiny2313a/4313 note: a = address high bits, b = address low bits, h = 0 - low byte, 1 - high byte, o = data out, i = data in, x = don?t care read extended fuse bits 0101 0000 0000 1000 xxxx xxxx oooo oooo read extended fuse bits. ?0? = pro- grammed, ?1? = unprogrammed. read calibration byte 0011 1000 000x xxxx 0000 000 b oooo oooo read calibration byte at address b . poll rdy/bsy 1111 0000 0000 0000 xxxx xxxx xxxx xxx o if o = ?1?, a programming operation is still busy. wait until this bit returns to ?0? before applying another command. table 20-15. serial programming instruction set instruction instruction format operation byte 1 byte 2 byte 3 byte4
195 8246a?avr?11/09 attiny2313a/4313 21. electrical characteristics 21.1 absolute maximum ratings* 21.2 dc characteristics operating temperature.................................. -55 c to +125 c *notice: stresses beyond those listed under ?absolute maximum ratings? may cause permanent dam- age to the device. this is a stress rating only and functional operation of the device at these or other conditions beyond those indicated in the operational sections of th is specification is not implied. exposure to absolute maximum rating conditions for extended periods may affect device reliability. storage temperature ..................................... -65c to +150c voltage on any pin except reset with respect to ground ................................-0.5v to v cc +0.5v voltage on reset with respect to ground......-0.5v to +13.0v maximum operating voltage ............................................ 6.0v dc current per i/o pin ............................................... 40.0 ma dc current v cc and gnd pins................................ 200.0 ma t a = -40 c to 85 c, v cc = 1.8v to 5.5v (unless otherwise noted) symbol parameter condition min. typ. max. units v il input low voltage except xtal1 and reset pin v cc = 1.8v - 2.4v v cc = 2.4v - 5.5v -0.5 0.2v cc 0.3v cc v v ih input high-voltage except xtal1 and reset pins v cc = 1.8v - 2.4v v cc = 2.4v - 5.5v 0.7v cc (1) 0.6v cc (1) v cc +0.5 (2) v v il1 input low voltage xtal1 pin v cc = 1.8v - 5.5v -0.5 0.1v cc v v ih1 input high-voltage xtal1 pin v cc = 1.8v - 2.4v v cc = 2.4v - 5.5v 0.8v cc (1) 0.7v cc (1) v cc +0.5 (2) v v il2 input low voltage reset pin v cc = 1.8v - 5.5v -0.5 0.2v cc v v ih2 input high-voltage reset pin v cc = 1.8v - 5.5v 0.9v cc (1) v cc +0.5 (2) v v il3 input low voltage reset pin as i/o v cc = 1.8v - 2.4v v cc = 2.4v - 5.5v -0.5 0.2v cc 0.3v cc v v ih3 input high-voltage reset pin as i/o v cc = 1.8v - 2.4v v cc = 2.4v - 5.5v 0.7v cc (1) 0.6v cc (1) v cc +0.5 (2) v v ol output low voltage (3) (except reset pin) (5) i ol = 20 ma, v cc = 5v i ol = 10ma, v cc = 3v 0.8 0.6 v v v oh output high-voltage (4) (except reset pin) (5) i oh = -20 ma, v cc = 5v i oh = -10 ma, v cc = 3v 4.2 2.4 v v i il input leakage current i/o pin v cc = 5.5 v, pin low (absolute value) 1 (6) a i ih input leakage current i/o pin v cc = 5.5 v, pin high (absolute value) 1 (6) a r rst reset pull-up resistor 30 60 k r pu i/o pin pull-up resistor 20 50 k
196 8246a?avr?11/09 attiny2313a/4313 notes: 1. ?min? means the lowest value wher e the pin is guaranteed to be read as high. 2. ?max? means the highest value where the pin is guaranteed to be read as low. 3. although each i/o port can sink more than the test conditions (20 ma at v cc = 5v, 10 ma at v cc = 3v) under steady state conditions (non-transient), t he following must be observed: 1] the sum of all iol, for al l ports, should not exceed 60 ma. if iol exceeds the test conditio n, vol may exceed the related sp ecification. pins ar e not guaranteed to sink current greater than the listed test condition. 4. although each i/o port can source more than the test conditions (20 ma at v cc = 5v, 10 ma at v cc = 3v) under steady state conditions (non-transient), t he following must be observed: 1] the sum of all ioh, for all ports, should not exceed 60 ma. if ioh exceeds the test condition, voh may exceed the relat ed specification. pins are not guaranteed to source current greater than the listed test condition. 5. the reset pin must tolerate high voltages when entering and op erating in programming modes and, as a consequence, has a weak drive strength as compared to regular i/o pins. see figure 22-29 and figure 22-30 . 6. these are test limits, which account for leakage currents of th e test environment. actual device leakage currents are lower. 7. values using methods described in ?minimizing power consumption? on page 35 . power reduction is enabled (prr = 0xff), the external clock is selected (c ksel = 0000), and there is no i/o drive. 8. bod disabled. 21.3 speed grades the maximum operating frequency of the device depends on v cc. as shown in figure 21-1 , the relationship between maximum frequency and v cc is linear in the region 1.8v < v cc < 4.5v. figure 21-1. maximum frequency vs. v cc i cc power supply current active 1mhz, v cc = 2v (7) 0.2 0.55 ma active 4mhz, v cc = 3v (7) 1.3 2.5 ma active 8mhz, v cc = 5v (7) 3.9 7 ma idle 1mhz, v cc = 2v (7) 0.03 0.15 ma idle 4mhz, v cc = 3v (7) 0.25 0.6 ma idle 8mhz, v cc = 5v (7) 12ma power-down mode wdt enabled, v cc = 3v (8) 410a wdt disabled, v cc = 3v (8) < 0.15 2 a t a = -40 c to 85 c, v cc = 1.8v to 5.5v (unless otherwise noted) (continued) symbol parameter condition min. typ. max. units 4 mhz 1. 8 v 5.5v 4.5v 20 mhz
197 8246a?avr?11/09 attiny2313a/4313 21.4 clock characteristics 21.4.1 calibrated internal rc oscillator accuracy it is possible to manua lly calibrate the internal oscillator to be more accu rate than def ault factory calibration. note that the osc illator frequency depend s on temperat ure and voltage. voltage and temperature characteristics can be found in figure 22-46 on page 226 , and figure 22-47 on page 227 . notes: 1. accuracy of oscillator frequency at calibrat ion point (fixed temperature and fixed voltage). 21.4.2 external clock drive figure 21-2. external clock drive waveform table 21-1. calibration accuracy of internal rc oscillator calibration method target frequency v cc temperature accuracy at given voltage & temperature (1) factory calibration 4.0 / 8.0mhz 3v 25 c10% user calibration fixed frequency within: 3.1 ? 4.7 mhz / 7.3 ? 9.1mhz fixed voltage within: 1.8v ? 5.5v fixed temperature within: -40 c ? 85 c 2% v il1 v ih1 table 21-2. external clock drive symbol parameter v cc = 1.8 - 5.5v v cc = 2.7 - 5.5v v cc = 4.5 - 5.5v units min. max. min. max. min. max. 1/t clcl clock frequency 0 4 0 10 0 20 mhz t clcl clock period 250 100 50 ns t chcx high time 100 40 20 ns t clcx low time 100 40 20 ns t clch rise time 2.0 1.6 0.5 s t chcl fall time 2.0 1.6 0.5 s t clcl change in period fr om one clock cycle to the next 2 2 2 %
198 8246a?avr?11/09 attiny2313a/4313 21.5 system and reset characteristics notes: 1. when reset pin used as reset (not as i/o). 2. not tested in production. 21.5.1 enhanced power-on reset notes: 1. values are guidelines, only. 2. threshold where device is released from reset when voltage is rising. 3. the power-on reset will not work unless the supply voltage has been below v poa . 21.5.2 brown-out detection note: 1. v bot may be below nominal minimum operating voltage for some devices. for devices where this is the case, the device is tested down to v cc = v bot during the production test. this guar- antees that a brown-out reset will occur before v cc drops to a voltage where correct operation of the microcontroller is no longer guaranteed. table 21-3. reset, brown-out, and internal voltage characteristics symbol parameter condi tion min typ max units v rst reset pin threshold voltage 0.2 v cc 0.8v cc v t rst minimum pulse width on reset pin (1)(2) v cc = 1.8 - 5.5v 2.5 s v hyst brown-out detector hysteresis (2) 50 mv t bod min pulse width on brown-out reset (2) 2s v bg internal bandgap reference voltage v cc = 2.7v t a = 25c 1.0 1.1 1.2 v t bg internal bandgap reference start-up time (2) v cc = 2.7v t a = 25c 40 70 s i bg internal bandgap reference current consumption (2) v cc = 2.7v t a = 25c 15 a table 21-4. characteristics of enhanced power-on reset. t a = -40 ? 85 c symbol parameter min (1) typ (1) max (1) units v por release threshold of power-on reset (2) 1.1 1.4 1.6 v v poa activation threshold of power-on reset (3) 0.6 1.3 1.6 v sr on power-on slope rate 0.01 v/ms table 21-5. v bot vs. bodlevel fuse coding bodlevel [1:0] fuses min (1) typ (1) max (1) units 11 bod disabled 10 1.7 1.8 2.0 v 01 2.5 2.7 2.9 00 4.1 4.3 4.5
199 8246a?avr?11/09 attiny2313a/4313 21.6 analog comparat or characteristics note: all parameters are based on simulation re sults and they are not tested in production table 21-6. analog comparator characteristics, t a = -40 c - 85 c symbol parameter condition min typ max units v acio input offset voltage v cc = 5v, v in = v cc / 2 < 10 40 mv i aclk input leakage current v cc = 5v, v in = v cc / 2 -50 50 na t acpd analog propagation delay (from saturation to slight overdrive) v cc = 2.7v 750 ns v cc = 4.0v 500 analog propagation delay (large step change) v cc = 2.7v 100 v cc = 4.0v 75 t dpd digital propagation delay v cc = 1.8v - 5.5 1 2 clk
200 8246a?avr?11/09 attiny2313a/4313 21.7 parallel programmi ng characteristics notes: 1. t wlrh is valid for the write flash, write eeprom, write fuse bits and write lock bits commands. 2. t wlrh_ce is valid for the chip erase command. table 21-7. parallel programming characteristics, v cc = 5v 10% symbol parameter min typ max units v pp programming enable voltage 11.5 12.5 v i pp programming enable current 250 a t dvxh data and control valid before xtal1 high 67 ns t xlxh xtal1 low to xtal1 high 200 ns t xhxl xtal1 pulse width high 150 ns t xldx data and control hold after xtal1 low 67 ns t xlwl xtal1 low to wr low 0 ns t xlph xtal1 low to pagel high 0 ns t plxh pagel low to xtal1 high 150 ns t bvph bs1 valid before pagel high 67 ns t phpl pagel pulse width high 150 ns t plbx bs1 hold after pagel low 67 ns t wlbx bs2/1 hold after wr low 67 ns t plwl pagel low to wr low 67 ns t bvwl bs1 valid to wr low 67 ns t wlwh wr pulse width low 150 ns t wlrl wr low to rdy/bsy low 0 1 s t wlrh wr low to rdy/bsy high (1) 3.7 4.5 ms t wlrh_ce wr low to rdy/bsy high for chip erase (2) 7.5 9 ms t xlol xtal1 low to oe low 0 ns t bvdv bs1 valid to data valid 0 1000 ns t oldv oe low to data valid 1000 ns t ohdz oe high to data tri-stated 1000 ns
201 8246a?avr?11/09 attiny2313a/4313 figure 21-3. parallel programming timing, including some general timing requirements figure 21-4. parallel programming timing, loading sequence with timing requirements (1) note: 1. the timing requirements shown in figure 21-3 (i.e., t dvxh , t xhxl , and t xldx ) also apply to load- ing operation. figure 21-5. parallel programming timing, reading sequence (within the same page) with timing requirements (1) note: 1. the timing requirements shown in figure 21-3 (i.e., t dvxh , t xhxl , and t xldx ) also apply to read- ing operation. data & contol (data, xa0/1, bs1, bs2) xtal1 t xhxl t wlwh t dvxh t xldx t plwl t wlrh wr rdy/bsy pagel t phpl t plbx t bvph t xlwl t wlbx t bvwl wlrl xtal1 pagel t plxh xlxh t t xlph addr0 (low byte) data (low byte) data (high byte) addr1 (low byte) data bs1 xa0 xa1 load address (low byte) load data (low byte) load data (high byte) load data load address (low byte) xtal1 oe addr0 (low byte) data (low byte) data (high byte) addr1 (low byte) data bs1 xa0 xa1 load address (low byte) read data (low byte) read data (high byte) load address (low byte) t bvdv t oldv t xlol t ohdz
202 8246a?avr?11/09 attiny2313a/4313 21.8 serial programming characteristics figure 21-6. serial programming timing note: 2 t clcl for f ck < 12 mhz, 3 t clcl for f ck >= 12 mhz figure 21-7. serial programming waveform table 21-8. serial programming characteristics, t a = -40 c to 85 c, v cc = 1.8 - 5.5v (unless otherwise noted) symbol parameter min typ max units 1/t clcl oscillator frequency (attiny2313a/4313) 0 4 mhz t clcl oscillator period (attiny2313a/4313) 250 ns 1/t clcl oscillator frequency (attiny2313a/4313, v cc = 4.5v - 5.5v) 020mhz t clcl oscillator period (attiny2313a/4313, v cc = 4.5v - 5.5v) 50 ns t shsl sck pulse width high 2 t clcl * ns t slsh sck pulse width low 2 t clcl * ns t ovsh mosi setup to sck high t clcl ns t shox mosi hold after sck high 2 t clcl ns t sliv sck low to miso valid 100 ns mosi miso sck t ovsh t shsl t slsh t shox msb msb lsb lsb serial clock input (sck) serial data input (mosi) (miso) sample serial data output
203 8246a?avr?11/09 attiny2313a/4313 22. typical characteristics the data contained in this section is largely ba sed on simulations and characterization of similar devices in the same process and design methods. thus, the data should be treated as indica- tions of how the part will behave. the following charts show typical behavior. t hese figures are not tested during manufacturing. during characterisation devices are operated at fr equencies higher than test limits but they are not guaranteed to function properly at frequenc ies higher than the ordering code indicates. all current consumption measurements are perfor med with all i/o pins configured as inputs and with internal pull-ups enabled. current consumption is a function of several factors such as oper- ating voltage, operating frequency, loading of i/o pins, switching rate of i/o pins, code executed and ambient temperature. the dominating factors are operating voltage and frequency. a sine wave generator with rail-to-rail output is used as clock source but current consumption in power-down mode is independent of clock select ion. the difference between current consump- tion in power-down mode with watchdog timer enabled and power-down mode with watchdog timer disabled represents the differenti al current drawn by the watchdog timer. the current drawn from pins with a capacitive lo ad may be estimated (for one pin) as follows: where v cc = operating voltage, c l = load capacitance and f sw = average switching frequency of i/o pin. 22.1 effect of power reduction peripheral modules are enabled and disabled via c ontrol bits in the power reduction register. see ?power reduction register? on page 34 for details. i cp v cc c l f sw table 22-1. additional current consumption (absolute) for peripherals of attiny2313a/4313 prr bit typical numbers v cc = 2v, f = 1mhz v cc = 3v, f = 4mhz v cc = 5v, f = 8mhz prtim0 2 a 11 a 50 a prtim1 5 a 30 a 120 a prusi 2 a 11 a 50 a prusart 4 a 22 a 95 a
204 8246a?avr?11/09 attiny2313a/4313 22.2 attiny2313a 22.2.1 current consumption in active mode figure 22-1. active supply current vs. low frequency (0.1 - 1.0 mhz) figure 22-2. active supply current vs . frequency (1 - 20 mhz) active supply current vs. low frequency (attiny2313a) (prr=0xff) 5.5 v 5.0 v 4.5 v 3.3 v 2.7 v 1.8 v 0 0,2 0,4 0,6 0,8 1 0 0,1 0,2 0,3 0,4 0,5 0,6 0,7 0,8 0,9 1 frequency (mhz) i cc (ma) active supply current vs. frequency (attiny2313a) (prr=0xff) 0 2 4 6 8 10 0 2 4 6 8 101214161820 frequency (mhz) i cc (ma) 1.8v 2.7v 3.3v 4.5v 5.0v 5.5v
205 8246a?avr?11/09 attiny2313a/4313 figure 22-3. active supply current vs. v cc (internal rc o scillator, 8 mhz) figure 22-4. active supply current vs. v cc (internal rc o scillator, 1 mhz) active supply current vs. v cc (attiny2313a) internal rc oscillator, 8 mhz 85 c 25 c -40 c 0 1 2 3 4 5 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) i cc (ma) active supply current vs. v cc (attiny2313a) internal rc oscillator, 1 mhz 85 c 25 c -40 c 0 0,2 0,4 0,6 0,8 1 1,2 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) i cc (ma)
206 8246a?avr?11/09 attiny2313a/4313 figure 22-5. active supply current vs. v cc (internal rc o scillator, 128 khz) 22.2.2 current consumption in idle mode figure 22-6. idle supply current vs. low frequency (0.1 - 1.0 mhz) active supply current vs. v cc (attiny2313a) internal rc oscillator, 128 khz 85 c 25 c -40 c 0 0,02 0,04 0,06 0,08 0,1 0,12 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) i cc (ma) idle supply current vs. low frequency (attiny2313a) (prr=0xff) 5.5 v 5.0 v 4.5 v 4.0 v 3.3 v 2.7 v 1.8 v 0 0,02 0,04 0,06 0,08 0,1 0,12 0,14 0,16 0 0,1 0,2 0,3 0,4 0,5 0,6 0,7 0,8 0,9 1 frequency (mhz) i cc (ma)
207 8246a?avr?11/09 attiny2313a/4313 figure 22-7. idle supply current vs. frequency (1 - 20 mhz) figure 22-8. idle supply current vs. v cc (internal rc o scillator, 8 mhz) idle supply current vs. frequency (attiny2313a) (prr=0xff) 5.5 v 5.0 v 4.5 v 4.0 v 3.3 v 2.7 v 1.8 v 0 0,5 1 1,5 2 2,5 3 0 2 4 6 8 101214161820 frequency (mhz) i cc (ma) idle supply current vs. v cc (attiny2313a) internal rc oscillator, 8 mhz 85 c 25 c -40 c 0 0,2 0,4 0,6 0,8 1 1,2 1,4 1,6 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) i cc (ma)
208 8246a?avr?11/09 attiny2313a/4313 figure 22-9. idle supply current vs. v cc (internal rc o scillator, 1 mhz) figure 22-10. idle supply current vs. v cc (internal rc o scillator, 128 khz) idle supply current vs. v cc (attiny2313a) internal rc oscillator, 1 mhz 85 c 25 c -40 c 0 0,05 0,1 0,15 0,2 0,25 0,3 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) i cc (ma) idle supply current vs. v cc (attiny2313a) internal rc oscillator, 128 khz 85 c 25 c -40 c 0 0,005 0,01 0,015 0,02 0,025 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) i cc (ma)
209 8246a?avr?11/09 attiny2313a/4313 22.2.3 current consumption in power-down mode figure 22-11. power-down supply current vs. v cc (watchdog timer disabled) figure 22-12. power-down supply current vs. v cc (watchdog timer enabled) power-down supply current vs. v cc (attiny2313a) watchdog timer disabled 85 c 25 c -40 c 0 0,1 0,2 0,3 0,4 0,5 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) i cc (ua) power-down supply current vs. v cc (attiny2313a) watchdog timer enabled 85 c 25 c -40 c 0 1 2 3 4 5 6 7 8 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) i cc (ua)
210 8246a?avr?11/09 attiny2313a/4313 22.2.4 current consumption in reset figure 22-13. reset supply current vs. v cc (0.1 - 1.0 mhz, excluding current through the reset pull-up) figure 22-14. reset supply current vs. v cc (1 - 20 mhz, excluding current through the reset pull-up) reset supply current vs. v cc (attiny2313a) excluding current through the reset pullup 5.5 v 5.0 v 4.5 v 4.0 v 3.3 v 2.7 v 1.8 v 0 0,02 0,04 0,06 0,08 0,1 0,12 0,14 0 0,1 0,2 0,3 0,4 0,5 0,6 0,7 0,8 0,9 1 frequency (mhz) i cc (ma) 5.5 v 5.0 v 4.5 v 4.0 v 3.3 v 2.7 v 1.8 v reset supply current vs. v cc (attiny2313a) excluding current through the reset pullup 0 0,2 0,4 0,6 0,8 1 1,2 1,4 1,6 1,8 2 2,2 0 2 4 6 8 101214161820 frequency (mhz) i cc (ma)
211 8246a?avr?11/09 attiny2313a/4313 22.2.5 current consumption of peripheral units figure 22-15. brownout detector current vs. v cc figure 22-16. programming current vs. v cc (attiny2313a) note: above programming current based on simulation and characterisation of similar device (attiny24a). brownout detector current vs. v cc (attiny2313a) bod level = 1.8v 85 c 25 c -40 c 0 5 10 15 20 25 30 35 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) i cc (ua) programming current vs. vcc 85 c 25 c -40 c 0 1000 2000 3000 4000 5000 6000 7000 8000 9000 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) i cc (ua)
212 8246a?avr?11/09 attiny2313a/4313 22.2.6 pull-up resistors figure 22-17. pull-up resistor current vs. input voltage (i/o pin, v cc = 1.8v) figure 22-18. pull-up resistor current vs. input voltage (i/o pin, v cc = 2.7v) i/o pin pull-up resistor current vs. input voltage (attiny2313a) 0 10 20 30 40 50 60 0 0,2 0,4 0,6 0,8 1 1,2 1,4 1,6 1,8 2 v op (v) i op (ua) 85 c 25 c -40 c 85 c 25 c -40 c i/o pin pull-up resistor current vs. input voltage (attiny2313a) 0 10 20 30 40 50 60 70 80 90 3 5 , 2 2 5 , 1 1 5 , 0 0 v op (v) i op (ua)
213 8246a?avr?11/09 attiny2313a/4313 figure 22-19. pull-up resistor current vs. input voltage (i/o pin, v cc = 5v) figure 22-20. reset pull-up resistor curr ent vs. reset pin voltage (v cc = 1.8v) 85 c 25 c -40 c i/o pin pull-up resistor current vs. input voltage (attiny2313a) 0 20 40 60 80 100 120 140 160 6 5 4 3 2 1 0 v op (v) i op (ua) reset pull-up resistor current vs. reset pin voltage (attiny2313a) -40 c 25 c 85 c 0 5 10 15 20 25 30 35 40 0 0,2 0,4 0,6 0,8 1 1,2 1,4 1,6 1,8 2 v reset (v) i reset (ua)
214 8246a?avr?11/09 attiny2313a/4313 figure 22-21. reset pull-up resistor curr ent vs. reset pin voltage (v cc = 2.7v) figure 22-22. reset pull-up resistor curr ent vs. reset pin voltage (v cc = 5v) -40 c 25 c 85 c reset pull-up resistor current vs. reset pin voltage (attiny2313a) 0 10 20 30 40 50 60 3 5 , 2 2 5 , 1 1 5 , 0 0 v reset (v) i reset (ua) -40 c 25 c 85 c reset pull-up resistor current vs. reset pin voltage (attiny2313a) 0 20 40 60 80 100 120 0 0,5 1 1,5 2 2,5 3 3,5 4 4,5 5 v reset (v) i reset (ua)
215 8246a?avr?11/09 attiny2313a/4313 22.2.7 output driver strength figure 22-23. v ol : output voltage vs. sink current (i/o pin, v cc = 1.8v) figure 22-24. v ol : output voltage vs. sink current (i/o pin, v cc = 3v) i/o pin output voltage vs. sink current (attiny2313a) vcc = 1.8v 85 c 25 c -40 c 0 0,05 0,1 0,15 0,2 0,25 0,3 0,35 0,4 0 0,5 1 1,5 2 2,5 3 3,5 4 4,5 5 i ol (ma) v ol (v) i/o pin output voltage vs. sink current (attiny2313a) vcc = 3v 85 c 25 c -40 c 0 0,2 0,4 0,6 0 1 8 6 4 2 0 i ol (ma) v ol (v)
216 8246a?avr?11/09 attiny2313a/4313 figure 22-25. v ol : output voltage vs. sink current (i/o pin, v cc = 5v) figure 22-26. v oh : output voltage vs. source current (i/o pin, v cc = 1.8v) i/o pin output voltage vs. sink current (attiny2313a) vcc = 5v 85 c 25 c -40 c 0 0,1 0,2 0,3 0,4 0,5 0,6 0 2 4 6 8 101214161820 i ol (ma) v ol (v) i/o pin output voltage vs. source current (attiny2313a) vcc = 1.8v 85 c 25 c -40 c 1,2 1,3 1,4 1,5 1,6 1,7 1,8 1,9 0 0,5 1 1,5 2 2,5 3 3,5 4 4,5 5 i oh (ma) v oh (v)
217 8246a?avr?11/09 attiny2313a/4313 figure 22-27. v oh : output voltage vs. source current (i/o pin, v cc = 3v) figure 22-28. v oh : output voltage vs. source current (i/o pin, v cc = 5v) i/o pin output voltage vs. source current (attiny2313a) vcc = 3v 85 c 25 c -40 c 2,5 2,6 2,7 2,8 2,9 3 3,1 0 1 8 6 4 2 0 i oh (ma) v oh (v) i/o pin output voltage vs. source current (attiny2313a) vcc = 5v 85 c 25 c -40 c 4,3 4,5 4,7 4,9 5,1 0 2 5 1 0 1 5 0 i oh (ma) v oh (v)
218 8246a?avr?11/09 attiny2313a/4313 figure 22-29. v ol : output voltage vs. sink current (reset pin as i/o, t = 25c) figure 22-30. v oh : output voltage vs. source current (reset pin as i/o, t = 25c) reset as i/o pin output voltage vs. sink current (attiny2313a) 0 0,1 0,2 0,3 0,4 0,5 0,6 0,7 0,8 0,9 1 4 3 2 1 0 i ol (ma) v ol (v) 5.0 v 3.0 v 1.8 v reset as i/o pin output voltage vs. source current (attiny2313a) 5.0 v 3.0 v 1.8 v 0 1 2 3 4 5 1 8 , 0 6 , 0 4 , 0 2 , 0 0 i oh (ma) v oh (v)
219 8246a?avr?11/09 attiny2313a/4313 22.2.8 input thresholds and hysteresis (for i/o ports) figure 22-31. v ih : input threshold voltage vs. v cc (i/o pin read as ?1?) figure 22-32. v il : input threshold voltage vs. v cc (i/o pin, read as ?0?) i/o pin input threshold voltage vs. v cc (attiny2313a) vih, io pin read as '1' 0 0,5 1 1,5 2 2,5 3 3,5 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) threshold (v) 85 c 25 c -40 c i/o pin input threshold voltage vs. v cc (attiny2313a) vil, io pin read as '0' 0 0,5 1 1,5 2 2,5 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) threshold (v) 85 c 25 c -40 c
220 8246a?avr?11/09 attiny2313a/4313 figure 22-33. v ih -v il : input hysteresis vs. v cc (i/o pin) figure 22-34. v ih : input threshold voltage vs. v cc (reset pin as i/o, read as ?1?) i/o pin input hysteresis vs. v cc (attiny2313a) 85 c 25 c -40 c 0 0,1 0,2 0,3 0,4 0,5 0,6 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) input hysteresis (mv) reset pin as i/o threshold voltage vs. v cc (attiny2313a) vih, reset read as '1' 85 c 25 c -40 c 0 0,5 1 1,5 2 2,5 3 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) threshold (v)
221 8246a?avr?11/09 attiny2313a/4313 figure 22-35. v il : input threshold voltage vs. v cc (reset pin as i/o, read as ?0?) figure 22-36. v ih -v il : input hysteresis vs. v cc (reset pin as i/o) reset pin as i/o threshold voltage vs. v cc (attiny2313a) vil, reset read as '0' 0 0,5 1 1,5 2 2,5 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) threshold (v) 85 c 25 c -40 c reset pin as io, input hysteresis vs. v cc (attiny2313a) vil, io pin read as "0" 85 c 25 c -40 c 0 0,1 0,2 0,3 0,4 0,5 0,6 0,7 0,8 0,9 1 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) input hysteresis (v)
222 8246a?avr?11/09 attiny2313a/4313 22.2.9 bod, bandgap and reset figure 22-37. bod thresholds vs. temperature (bod level is 4.3v) figure 22-38. bod thresholds vs. temperature (bod level is 2.7v) bod thresholds vs. temperature (bod level set to 4.3v) (attiny2313a) bodlevel = 4.3v 4,22 4,24 4,26 4,28 4,3 4,32 4,34 4,36 -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100 temperature (c) threshold (v) v cc rising v cc falling bod thresholds vs. temperature (bod level set to 2.7v) (attiny2313a) bodlevel = 2.7v 2,66 2,68 2,7 2,72 2,74 2,76 2,78 -40 -20 0 20 40 60 80 100 temperature (c) threshold (v) v cc rising v cc falling
223 8246a?avr?11/09 attiny2313a/4313 figure 22-39. bod thresholds vs. temperature (bod level is 1.8v) figure 22-40. bandgap voltage vs. supply voltage bod thresholds vs. temperature (bod level set to 1.8v) (attiny2313a) bodlevel = 1.8v 1,78 1,79 1,8 1,81 1,82 1,83 1,84 -40 -20 0 20 40 60 80 100 temperature (c) threshold (v) v cc rising v cc falling bandgap voltage vs. v cc (attiny2313a) calibrated 0,95 1 1,05 1,1 1,15 1,2 1,5 2 2,5 3 3,5 4 4,5 5 5,5 vcc (v) bandgap voltage (v)
224 8246a?avr?11/09 attiny2313a/4313 figure 22-41. bandgap voltage vs. temperature figure 22-42. v ih : input threshold voltage vs. v cc (reset pin, read as ?1?) bandgap voltage vs. temp (attiny2313a) (vcc=5v) calibrated 1 1,02 1,04 1,06 1,08 1,1 1,12 1,14 1,16 -40 -20 0 20 40 60 80 100 temperature bandgap voltage (v) reset input threshold voltage vs. v cc (attiny2313a) vih, io pin read as '1' 85 c 25 c -40 c 0 0,5 1 1,5 2 2,5 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) threshold (v)
225 8246a?avr?11/09 attiny2313a/4313 figure 22-43. v il : input threshold voltage vs. v cc (reset pin, read as ?0?) figure 22-44. v ih -v il : input hysteresis vs. v cc (reset pin) reset input threshold voltage vs. v cc (attiny2313a) vil, io pin read as '0' 85 c 25 c -40 c 0 0,5 1 1,5 2 2,5 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) threshold (v) reset pin input hysteresis vs. v cc (attiny2313a) 0 0,1 0,2 0,3 0,4 0,5 0,6 0,7 0,8 0,9 1 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) input hysteresis (mv) 85 c 25 c -40 c
226 8246a?avr?11/09 attiny2313a/4313 figure 22-45. minimum reset pulse width vs. v cc 22.2.10 internal oscillator speed figure 22-46. calibrated 8 mhz rc osc illator frequency vs. v cc minimum reset pulse width vs. v cc (attiny2313a) 85 c 25 c -40 c 0 200 400 600 800 1000 1200 1400 1600 1800 2000 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) pulsewidth (ns) calibrated 8.0mhz rc oscillator frequency vs. operating voltage (attiny2313a) 7 7,2 7,4 7,6 7,8 8 8,2 8,4 8,6 8,8 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) f rc (mhz) 85 c 25 c -40 c
227 8246a?avr?11/09 attiny2313a/4313 figure 22-47. calibrated 8 mhz rc oscillato r frequency vs. temperature figure 22-48. calibrated 8 mhz rc oscillato r frequency vs. osccal value calibrated 8.0mhz rc oscillator frequency vs. temperature (attiny2313a) 5.0 v 3.0 v 1.8 v 7 7,5 8 8,5 9 -40 -20 0 20 40 60 80 100 temperature f rc (mhz) calibrated 8.0mhz rc oscillator frequency vs. osccal value (attiny2313a) (vcc=3v) 85 c 25 c -40 c 0 2 4 6 8 10 12 14 0 163248648096112 osccal (x1) f rc (mhz)
228 8246a?avr?11/09 attiny2313a/4313 22.3 attiny4313 22.3.1 current consumption in active mode figure 22-49. active supply current vs. low frequency (0.1 - 1.0 mhz) figure 22-50. active supply current vs . frequency (1 - 20 mhz) active supply current vs. low frequency (attiny4313) (prr=0xff) 5.5 v 5.0 v 4.5 v 3.3 v 2.7 v 1.8 v 0 0,2 0,4 0,6 0,8 1 0 0,1 0,2 0,3 0,4 0,5 0,6 0,7 0,8 0,9 1 frequency (mhz) i cc (ma) active supply current vs. frequency (attiny4313) (prr=0xff) 0 2 4 6 8 10 02468101214161820 frequency (mhz) i cc (ma) 1.8v 2.7v 3.3v 4.5v 5.0v 5.5v
229 8246a?avr?11/09 attiny2313a/4313 figure 22-51. active supply current vs. v cc (internal rc o scillator, 8 mhz) figure 22-52. active supply current vs. v cc (internal rc o scillator, 1 mhz) active supply current vs. v cc (attiny4313) internal rc oscillator, 8 mhz 85 c 25 c -40 c 0 1 2 3 4 5 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) i cc (ma) active supply current vs. v cc (attiny4313) internal rc oscillator, 1 mhz 85 c 25 c -40 c 0 0,2 0,4 0,6 0,8 1 1,2 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) i cc (ma)
230 8246a?avr?11/09 attiny2313a/4313 figure 22-53. active supply current vs. v cc (internal rc o scillator, 128 khz) 22.3.2 current consumption in idle mode figure 22-54. idle supply current vs. low frequency (0.1 - 1.0 mhz) active supply current vs. v cc (attiny4313) internal rc oscillator, 128 khz 85 c 25 c -40 c 0 0,02 0,04 0,06 0,08 0,1 0,12 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) i cc (ma) idle supply current vs. low frequency (attiny4313) (prr=0xff) 5.5 v 5.0 v 4.5 v 4.0 v 3.3 v 2.7 v 1.8 v 0 0,02 0,04 0,06 0,08 0,1 0,12 0,14 0,16 0 0,1 0,2 0,3 0,4 0,5 0,6 0,7 0,8 0,9 1 frequency (mhz) i cc (ma)
231 8246a?avr?11/09 attiny2313a/4313 figure 22-55. idle supply current vs. frequency (1 - 20 mhz) figure 22-56. idle supply current vs. v cc (internal rc o scillator, 8 mhz) idle supply current vs. frequency (attiny4313) (prr=0xff) 0 0,5 1 1,5 2 2,5 3 0 2 4 6 8 101214161820 frequency (mhz) i cc (ma) 5.5 v 5.0 v 4.5 v 4.0 v 3.3 v 2.7 v 1.8 v idle supply current vs. v cc (attiny4313) internal rc oscillator, 8 mhz 85 c 25 c -40 c 0 0,2 0,4 0,6 0,8 1 1,2 1,4 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) i cc (ma)
232 8246a?avr?11/09 attiny2313a/4313 figure 22-57. idle supply current vs. v cc (internal rc o scillator, 1 mhz) figure 22-58. idle supply current vs. v cc (internal rc o scillator, 128 khz) idle supply current vs. v cc (attiny4313) internal rc oscillator, 1 mhz 85 c 25 c -40 c 0 0,1 0,2 0,3 0,4 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) i cc (ma) idle supply current vs. v cc (attiny4313) internal rc oscillator, 128 khz 85 c 25 c -40 c 0 0,005 0,01 0,015 0,02 0,025 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) i cc (ma)
233 8246a?avr?11/09 attiny2313a/4313 22.3.3 current consumption in power-down mode figure 22-59. power-down supply current vs. v cc (watchdog timer disabled) figure 22-60. power-down supply current vs. v cc (watchdog timer enabled) power-down supply current vs. v cc (attiny4313) watchdog timer disabled 85 c 25 c -40 c 0 0,1 0,2 0,3 0,4 0,5 0,6 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) i cc (ua) power-down supply current vs. v cc (attiny4313) watchdog timer enabled 85 c 25 c -40 c 0 1 2 3 4 5 6 7 8 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) i cc (ua)
234 8246a?avr?11/09 attiny2313a/4313 22.3.4 current consumption in reset figure 22-61. reset supply current vs. v cc (0.1 - 1.0 mhz, excluding current through the reset pull-up) figure 22-62. reset supply current vs. v cc (1 - 20 mhz, excluding current through the reset pull-up) reset supply current vs. v cc (attiny4313) excluding current through the reset pullup 5.5 v 5.0 v 4.5 v 4.0 v 3.3 v 2.7 v 1.8 v 0 0,02 0,04 0,06 0,08 0,1 0,12 0 0,1 0,2 0,3 0,4 0,5 0,6 0,7 0,8 0,9 1 frequency (mhz) i cc (ma) reset supply current vs. v cc (attiny4313) excluding current through the reset pullup 0 0,2 0,4 0,6 0,8 1 1,2 1,4 1,6 1,8 2 2,2 0 2 4 6 8 10 12 14 16 18 20 frequency (mhz) i cc (ma) 5.5 v 5.0 v 4.5 v 4.0 v 3.3 v 2.7 v 1.8 v
235 8246a?avr?11/09 attiny2313a/4313 22.3.5 current consumption of peripheral units figure 22-63. brownout detector current vs. v cc figure 22-64. programming current vs. v cc (attiny4313) note: above programming current based on simulation and characterisation of similar device (attiny44a). brownout detector current vs. v cc (attiny4313) bod level = 1.8v 85 c 25 c -40 c 0 5 10 15 20 25 30 35 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) i cc (ua) programming current vs. vcc 85 c 25 c -40 c 0 2000 4000 6000 8000 10000 12000 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) i cc (ua)
236 8246a?avr?11/09 attiny2313a/4313 22.3.6 pull-up resistors figure 22-65. pull-up resistor current vs. input voltage (i/o pin, v cc = 1.8v) figure 22-66. pull-up resistor current vs. input voltage (i/o pin, v cc = 2.7v) i/o pin pull-up resistor current vs. input voltage (attiny4313) 0 10 20 30 40 50 60 0 0,2 0,4 0,6 0,8 1 1,2 1,4 1,6 1,8 2 v op (v) i op (ua) 85 c 25 c -40 c i/o pin pull-up resistor current vs. input voltage (attiny4313) 0 10 20 30 40 50 60 70 80 3 5 , 2 2 5 , 1 1 5 , 0 0 v op (v) i op (ua) 85 c 25 c -40 c
237 8246a?avr?11/09 attiny2313a/4313 figure 22-67. pull-up resistor current vs. input voltage (i/o pin, v cc = 5v) figure 22-68. reset pull-up resistor curr ent vs. reset pin voltage (v cc = 1.8v) i/o pin pull-up resistor current vs. input voltage (attiny4313) 0 20 40 60 80 100 120 140 160 6 5 4 3 2 1 0 v op (v) i op (ua) 85 c 25 c -40 c reset pull-up resistor current vs. reset pin voltage (attiny4313) 0 5 10 15 20 25 30 35 40 0 0,2 0,4 0,6 0,8 1 1,2 1,4 1,6 1,8 2 v reset (v) i reset (ua) 85 c 25 c -40 c
238 8246a?avr?11/09 attiny2313a/4313 figure 22-69. reset pull-up resistor curr ent vs. reset pin voltage (v cc = 2.7v) figure 22-70. reset pull-up resistor curr ent vs. reset pin voltage (v cc = 5v) reset pull-up resistor current vs. reset pin voltage (attiny4313) 0 10 20 30 40 50 60 3 5 , 2 2 5 , 1 1 5 , 0 0 v reset (v) i reset (ua) 85 c 25 c -40 c reset pull-up resistor current vs. reset pin voltage (attiny4313) 0 20 40 60 80 100 120 6 5 4 3 2 1 0 v reset (v) i reset (ua)
239 8246a?avr?11/09 attiny2313a/4313 22.3.7 output driver strength figure 22-71. v ol : output voltage vs. sink current (i/o pin, v cc = 1.8v) figure 22-72. v ol : output voltage vs. sink current (i/o pin, v cc = 3v) i/o pin output voltage vs. sink current (attiny4313) 0 0,05 0,1 0,15 0,2 0,25 0,3 0,35 0,4 0 0,5 1 1,5 2 2,5 3 3,5 4 4,5 5 i ol (ma) v ol (v) v cc = 1.8v 85 c 25 c -40 c i/o pin output voltage vs. sink current (attiny4313) 0 0,1 0,2 0,3 0,4 0,5 0,6 0 1 8 6 4 2 0 i ol (ma) v ol (v) v cc = 3v 85 c 25 c -40 c
240 8246a?avr?11/09 attiny2313a/4313 figure 22-73. v ol : output voltage vs. sink current (i/o pin, v cc = 5v) figure 22-74. v oh : output voltage vs. source current (i/o pin, v cc = 1.8v) i/o pin output voltage vs. sink current (attiny4313) 85 c 25 c -40 c 0 0,1 0,2 0,3 0,4 0,5 0,6 0 2 4 6 8 101214161820 i ol (ma) v ol (v) v cc = 5v i/o pin output voltage vs. source current (attiny4313) 1 1,2 1,4 1,6 1,8 2 0 0,5 1 1,5 2 2,5 3 3,5 4 4,5 5 i oh (ma) v oh (v) v cc = 1.8v 85 c 25 c -40 c
241 8246a?avr?11/09 attiny2313a/4313 figure 22-75. v oh : output voltage vs. source current (i/o pin, v cc = 3v) figure 22-76. v oh : output voltage vs. source current (i/o pin, v cc = 5v) i/o pin output voltage vs. source current (attiny4313) 2,5 2,6 2,7 2,8 2,9 3 3,1 0 1 8 6 4 2 0 i oh (ma) v oh (v) v cc = 3v 85 c 25 c -40 c i/o pin output voltage vs. source current (attiny4313) 85 c 25 c -40 c 4,3 4,5 4,7 4,9 5,1 0 2 5 1 0 1 5 0 i oh (ma) v oh (v) v cc = 5v
242 8246a?avr?11/09 attiny2313a/4313 figure 22-77. v ol : output voltage vs. sink current (reset pin as i/o, t = 25c) figure 22-78. v oh : output voltage vs. source current (reset pin as i/o, t = 25c) reset as i/o pin output voltage vs. sink current (attiny4313) 0 0,1 0,2 0,3 0,4 0,5 0,6 0,7 0,8 0,9 1 4 3 2 1 0 i ol (ma) v ol (v) 5.0 v 3.0 v 1.8 v reset as i/o pin output voltage vs. source current (attiny4313) 0 1 2 3 4 5 1 8 , 0 6 , 0 4 , 0 2 , 0 0 i oh (ma) v oh (v) 5.0v 3.0v 1.8v
243 8246a?avr?11/09 attiny2313a/4313 22.3.8 input thresholds and hysteresis (for i/o ports) figure 22-79. v ih : input threshold voltage vs. v cc (i/o pin read as ?1?) figure 22-80. v il : input threshold voltage vs. v cc (i/o pin, read as ?0?) i/o pin input threshold voltage vs. v cc (attiny4313) vih, io pin read as '1' 85 c 25 c -40 c 0 0,5 1 1,5 2 2,5 3 3,5 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) threshold (v) i/o pin input threshold voltage vs. v cc (attiny4313) vil, io pin read as '0' 85 c 25 c -40 c 0 0,5 1 1,5 2 2,5 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) threshold (v)
244 8246a?avr?11/09 attiny2313a/4313 figure 22-81. v ih -v il : input hysteresis vs. v cc (i/o pin) figure 22-82. v ih : input threshold voltage vs. v cc (reset pin as i/o, read as ?1?) i/o pin input hysteresis vs. v cc (attiny4313) 85 c 25 c -40 c 0 0,1 0,2 0,3 0,4 0,5 0,6 1,522,533,544,555,5 v cc (v) input hysteresis (mv) reset pin as i/o threshold voltage vs. v cc (attiny4313) vih, reset read as '1' 85 c 25 c -40 c 0 0,5 1 1,5 2 2,5 3 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) threshold (v)
245 8246a?avr?11/09 attiny2313a/4313 figure 22-83. v il : input threshold voltage vs. v cc (reset pin as i/o, read as ?0?) figure 22-84. v ih -v il : input hysteresis vs. v cc (reset pin as i/o) reset pin as i/o threshold voltage vs. v cc (attiny4313) vil, reset read as '0' 85 c 25 c -40 c 0 0,5 1 1,5 2 2,5 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) threshold (v) reset pin as io, input hysteresis vs. v cc (attiny4313) vil, io pin read as "0" 85 c 25 c -40 c 0 0,1 0,2 0,3 0,4 0,5 0,6 0,7 0,8 0,9 1 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) input hysteresis (v)
246 8246a?avr?11/09 attiny2313a/4313 22.3.9 bod, bandgap and reset figure 22-85. bod thresholds vs. temperature (bod level is 4.3v) figure 22-86. bod thresholds vs. temperature (bod level is 2.7v) bod thresholds vs. temperature (bod level set to 4.3v) (attiny4313) bod level = 4.3v 4,16 4,18 4,2 4,22 4,24 4,26 4,28 4,3 4,32 4,34 4,36 4,38 -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100 temperature (c) threshold (v) v cc rising v cc falling bod thresholds vs. temperature (bod level set to 2.7v) (attiny4313) bod level = 2.7v 2,62 2,64 2,66 2,68 2,7 2,72 2,74 2,76 2,78 -40 -20 0 20 40 60 80 100 temperature (c) threshold (v) v cc rising v cc falling
247 8246a?avr?11/09 attiny2313a/4313 figure 22-87. bod thresholds vs. temperature (bod level is 1.8v) figure 22-88. bandgap voltage vs. supply voltage 1,76 1,77 1,78 1,79 1,8 1,81 1,82 1,83 1,84 -40 -20 0 20 40 60 80 100 temperature (c) threshold (v) bod thresholds vs. temperature (bod level set to 1.8v) (attiny4313) bod level = 1.8v v cc rising v cc falling bandgap voltage vs. v cc (attiny4313) calibrated 0,95 1 1,05 1,1 1,15 1,2 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) bandgap voltage (v)
248 8246a?avr?11/09 attiny2313a/4313 figure 22-89. bandgap voltage vs. temperature figure 22-90. v ih : input threshold voltage vs. v cc (reset pin, read as ?1?) bandgap voltage vs. temp (attiny4313) (vcc=5v) calibrated 1 1,02 1,04 1,06 1,08 1,1 1,12 1,14 -40 -20 0 20 40 60 80 100 temperature bandgap voltage (v) reset input threshold voltage vs. v cc (attiny4313) vih, io pin read as '1' 85 c 25 c -40 c 0 0,5 1 1,5 2 2,5 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) threshold (v)
249 8246a?avr?11/09 attiny2313a/4313 figure 22-91. v il : input threshold voltage vs. v cc (reset pin, read as ?0?) figure 22-92. v ih -v il : input hysteresis vs. v cc (reset pin) reset input threshold voltage vs. v cc (attiny4313) vil, io pin read as '0' 85 c 25 c -40 c 0 0,5 1 1,5 2 2,5 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) threshold (v) reset pin input hysteresis vs. v cc (attiny4313) 85 c 25 c -40 c 0 0,1 0,2 0,3 0,4 0,5 0,6 0,7 0,8 0,9 1 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) input hysteresis (mv)
250 8246a?avr?11/09 attiny2313a/4313 figure 22-93. minimum reset pulse width vs. v cc 22.3.10 internal oscillator speed figure 22-94. calibrated 8 mhz rc osc illator frequency vs. v cc minimum reset pulse width vs. v cc (attiny4313) 85 c 25 c -40 c 0 200 400 600 800 1000 1200 1400 1600 1800 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) pulsewidth (ns) calibrated 8.0mhz rc oscillator frequency vs. operating voltage (attiny4313) 7,2 7,4 7,6 7,8 8 8,2 8,4 8,6 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) f rc (mhz) 85 c 25 c -40 c
251 8246a?avr?11/09 attiny2313a/4313 figure 22-95. calibrated 8 mhz rc oscillato r frequency vs. temperature figure 22-96. calibrated 8 mhz rc oscillato r frequency vs. osccal value calibrated 8.0mhz rc oscillator frequency vs. temperature (attiny4313) 5.0 v 3.0 v 1.8 v 7,4 7,6 7,8 8 8,2 8,4 8,6 -40 -20 0 20 40 60 80 100 temperature f rc (mhz) calibrated 8.0mhz rc oscillator frequency vs. osccal value (attiny4313) (vcc=3v) 85 c 25 c -40 c 0 2 4 6 8 10 12 14 0 163248648096112 osccal (x1) f rc (mhz)
252 8246a?avr?11/09 attiny2313a/4313 23. register summary address name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 page 0x3f (0x5f) sreg i t h s v n z c 8 0x3e (0x5e) reserved ? ? ? ? ? ? ? ? 0x3d (0x5d) spl sp7 sp6 sp5 sp4 sp3 sp2 sp1 sp0 11 0x3c (0x5c) ocr0b timer/counter0 ? compare register b 85 0x3b (0x5b) gimsk int1 int0 pcie0 pcie2 pcie1 ? ? ?50 0x3a (0x5a) gifr intf1 intf0 pcif0 pcif2 pcif1 ? ? ?51 0x39 (0x59) timsk toie1 ocie1a ocie1b ? icie1 ocie0b toie0 ocie0a 86, 115 0x38 (0x58) tifr tov1 ocf1a ocf1b ? icf1 ocf0b tov0 ocf0a 86, 115 0x37 (0x57) spmcsr ? ? rsig ctpb rflb pgwrt pgers spmen 176 0x36 (0x56) ocr0a timer/counter0 ? compare register a 85 0x35 (0x55) mcucr pud sm1 se sm0 isc11 isc10 isc01 isc00 36, 50, 68 0x34 (0x54) mcusr ? ? ? ? wdrf borf extrf porf 44 0x33 (0x53) tccr0b foc0a foc0b ? ? wgm02 cs02 cs01 cs00 84 0x32 (0x52) tcnt0 timer/counter0 (8-bit) 85 0x31 (0x51) osccal ? cal6 cal5 cal4 cal3 cal2 cal1 cal0 30 0x30 (0x50) tccr0a com0a1 com0a0 com0b1 com0b0 ? ?wgm01wgm00 81 0x2f (0x4f) tccr1a com1a1 com1a0 com1b1 com1b0 ? ? wgm11 wgm10 110 0x2e (0x4e) tccr1b icnc1 ices1 ? wgm13 wgm12 cs12 cs11 cs10 112 0x2d (0x4d) tcnt1h timer/counter1 ? counter register high byte 114 0x2c (0x4c) tcnt1l timer/counter1 ? counter register low byte 114 0x2b (0x4b) ocr1ah timer/counter1 ? compare register a high byte 114 0x2a (0x4a) ocr1al timer/counter1 ? compare register a low byte 114 0x29 (0x49) ocr1bh timer/counter1 ? compare register b high byte 114 0x28 (0x48) ocr1bl timer/counter1 ? compare register b low byte 114 0x27 (0x47) reserved ? ? ? ? ? ? ? ? 0x26 (0x46) clkpr clkpce ? ? ? clkps3 clkps2 clkps1 clkps0 30 0x25 (0x45) icr1h timer/counter1 - input capture register high byte 114 0x24 (0x44) icr1l timer/counter1 - input capture register low byte 114 0x23 (0x43) gtccr ? ? ? ? ? ? ? psr10 118 0x22 (ox42) tccr1c foc1a foc1b ? ? ? ? ? ? 113 0x21 (0x41) wdtcsr wdif wdie wdp3 wdce wde wdp2 wdp1 wdp0 44 0x20 (0x40) pcmsk0 pcint7 pcint6 pcint5 pcint4 pcint3 pcint2 pcint1 pcint0 53 0x1f (0x3f) reserved ? ? ? ? ? ? ? ? 0x1e (0x3e) eear ? eeprom address register 22 0x1d (0x3d) eedr eeprom data register 22 0x1c (0x3c) eecr ? ? eepm1 eepm0 eerie eempe eepe eere 22 0x1b (0x3b) porta ? ? ? ? ? porta2 porta1 porta0 68 0x1a (0x3a) ddra ? ? ? ? ? dda2 dda1 dda0 68 0x19 (0x39) pina ? ? ? ? ? pina2 pina1 pina0 69 0x18 (0x38) portb portb7 portb6 portb5 portb4 portb3 portb2 portb1 portb0 69 0x17 (0x37) ddrb ddb7 ddb6 ddb5 ddb4 ddb3 ddb2 ddb1 ddb0 69 0x16 (0x36) pinb pinb7 pinb6 pinb5 pinb4 pinb3 pinb2 pinb1 pinb0 69 0x15 (0x35) gpior2 general purpose i/o register 2 23 0x14 (0x34) gpior1 general purpose i/o register 1 23 0x13 (0x33) gpior0 general purpose i/o register 0 23 0x12 (0x32) portd ? portd6 portd5 portd4 portd3 portd2 portd1 portd0 69 0x11 (0x31) ddrd ? ddd6 ddd5 ddd4 ddd3 ddd2 ddd1 ddd0 69 0x10 (0x30) pind ? pind6 pind5 pind4 pind3 pind2 pind1 pind0 69 0x0f (0x2f) usidr usi data register 165 0x0e (0x2e) usisr usisif usioif usipf usidc usicnt3 usicnt2 usicnt1 usicnt0 164 0x0d (0x2d) usicr usisie usioie usiwm1 usiwm0 usics1 usics0 usiclk usitc 162 0x0c (0x2c) udr uart data register (8-bit) 136 0x0b (0x2b) ucsra rxc txc udre fe dor upe u2x mpcm 137 0x0a (0x2a) ucsrb rxcie txcie udrie rxen txen ucsz2 rxb8 txb8 138 0x09 (0x29) ubrrl ubrrh[7:0] 140 0x08 (0x28) acsr acd acbg aco aci acie acic acis1 acis0 167 0x07 (0x27) bodcr ? ? ? ? ? ?bodsbodse 37 0x06 (0x26) prr ? ? ? ? prtim1 prtim0 prusi prusart 36 0x05 (0x25) pcmsk2 ? pcint17 pcint16 pcint15 pcint14 pcint13 pcint12 pcint11 52 0x04 (0x24) pcmsk1 ? ? ? ? ? pcint10 pcint9 pcint8 52 0x03 (0x23) ucsrc umsel1 umsel0 upm1 upm0 usbs ucsz1 ucsz0 ucpol 139 0x02 (0x22) ubrrh ? ? ? ? ubrrh[11:8] 140 0x01 (0x21) didr ? ? ? ? ? ? ain1d ain0d 168 0x00 (0x20) usibr usi buffer register 166
253 8246a?avr?11/09 attiny2313a/4313 notes: 1. for compatibility with future devices, reserved bits shou ld be written to zero if accesse d. reserved i/o memory address es should never be written. 2. i/o registers within the address range 0x00 - 0x1f are directly bit-accessible using the sbi and cbi instructions. in these registers, the value of single bits can be ch ecked by using the sbis and sbic instructions. 3. some of the status flags are cleared by writing a logical one to them. note that , unlike most other avrs, the cbi and sbi instructions will only operate on the specified bit, and can th erefore be used on registers containing such status flags. the cbi and sbi instructions work wit h registers 0x00 to 0x1f only. 4. when using the i/o specific commands in and out, the i/o addresses 0x00 - 0x3f must be used. when addressing i/o registers as data space using ld and st instructions, 0x20 must be added to these addresses.
254 8246a?avr?11/09 attiny2313a/4313 24. instruction set summary mnemonics operands description operation flags #clocks arithmetic and logic instructions add rd, rr add two registers rd rd + rr z,c,n,v,h 1 adc rd, rr add with carry two registers rd rd + rr + c z,c,n,v,h 1 adiw rdl,k add immediate to word rdh:rdl rdh:rdl + k z,c,n,v,s 2 sub rd, rr subtract two registers rd rd - rr z,c,n,v,h 1 subi rd, k subtract constant from register rd rd - k z,c,n,v,h 1 sbc rd, rr subtract with carry two registers rd rd - rr - c z,c,n,v,h 1 sbci rd, k subtract with carry constant from reg. rd rd - k - c z,c,n,v,h 1 sbiw rdl,k subtract immediate from word rdh:rdl rdh:rdl - k z,c,n,v,s 2 and rd, rr logical and registers rd rd ? rr z,n,v 1 andi rd, k logical and register and constant rd rd ? k z,n,v 1 or rd, rr logical or registers rd rd v rr z,n,v 1 ori rd, k logical or register and constant rd rd v k z,n,v 1 eor rd, rr exclusive or registers rd rd rr z,n,v 1 com rd one?s complement rd 0xff ? rd z,c,n,v 1 neg rd two?s complement rd 0x00 ? rd z,c,n,v,h 1 sbr rd,k set bit(s) in register rd rd v k z,n,v 1 cbr rd,k clear bit(s) in register rd rd ? (0xff - k) z,n,v 1 inc rd increment rd rd + 1 z,n,v 1 dec rd decrement rd rd ? 1 z,n,v 1 tst rd test for zero or minus rd rd ? rd z,n,v 1 clr rd clear register rd rd rd z,n,v 1 ser rd set register rd 0xff none 1 branch instructions rjmp k relative jump pc pc + k + 1 none 2 ijmp indirect jump to (z) pc z none 2 rcall k relative subroutine call pc pc + k + 1 none 3 icall indirect call to (z) pc znone3 ret subroutine return pc stack none 4 reti interrupt return pc stack i 4 cpse rd,rr compare, skip if equal if (rd = rr) pc pc + 2 or 3 none 1/2/3 cp rd,rr compare rd ? rr z, n,v,c,h 1 cpc rd,rr compare with carry rd ? rr ? c z, n,v,c,h 1 cpi rd,k compare register with immediate rd ? k z, n,v,c,h 1 sbrc rr, b skip if bit in register cleared if (rr(b)=0) pc pc + 2 or 3 none 1/2/3 sbrs rr, b skip if bit in register is set if (rr(b)=1) pc pc + 2 or 3 none 1/2/3 sbic p, b skip if bit in i/o register cleared if (p(b)=0) pc pc + 2 or 3 none 1/2/3 sbis p, b skip if bit in i/o register is set if (p(b)=1) pc pc + 2 or 3 none 1/2/3 brbs s, k branch if status flag set if (sreg(s) = 1) then pc pc+k + 1 none 1/2 brbc s, k branch if status flag cleared if (sreg(s) = 0) then pc pc+k + 1 none 1/2 breq k branch if equal if (z = 1) then pc pc + k + 1 none 1/2 brne k branch if not equal if (z = 0) then pc pc + k + 1 none 1/2 brcs k branch if carry set if (c = 1) then pc pc + k + 1 none 1/2 brcc k branch if carry cleared if (c = 0) then pc pc + k + 1 none 1/2 brsh k branch if same or higher if (c = 0) then pc pc + k + 1 none 1/2 brlo k branch if lower if (c = 1) then pc pc + k + 1 none 1/2 brmi k branch if minus if (n = 1) then pc pc + k + 1 none 1/2 brpl k branch if plus if (n = 0) then pc pc + k + 1 none 1/2 brge k branch if greater or equal, signed if (n v= 0) then pc pc + k + 1 none 1/2 brlt k branch if less than zero, signed if (n v= 1) then pc pc + k + 1 none 1/2 brhs k branch if half carry flag set if (h = 1) then pc pc + k + 1 none 1/2 brhc k branch if half carry flag cleared if (h = 0) then pc pc + k + 1 none 1/2 brts k branch if t flag set if (t = 1) then pc pc + k + 1 none 1/2 brtc k branch if t flag cleared if (t = 0) then pc pc + k + 1 none 1/2 brvs k branch if overflow flag is set if (v = 1) then pc pc + k + 1 none 1/2 brvc k branch if overflow flag is cleared if (v = 0) then pc pc + k + 1 none 1/2 brie k branch if interrupt enabled if ( i = 1) then pc pc + k + 1 none 1/2 brid k branch if interrupt disabled if ( i = 0) then pc pc + k + 1 none 1/2 bit and bit-test instructions sbi p,b set bit in i/o register i/o(p,b) 1none2 cbi p,b clear bit in i/o register i/o(p,b) 0none2 lsl rd logical shift left rd(n+1) rd(n), rd(0) 0 z,c,n,v 1 lsr rd logical shift right rd(n) rd(n+1), rd(7) 0 z,c,n,v 1 rol rd rotate left through carry rd(0) c,rd(n+1) rd(n),c rd(7) z,c,n,v 1
255 8246a?avr?11/09 attiny2313a/4313 ror rd rotate right through carry rd(7) c,rd(n) rd(n+1),c rd(0) z,c,n,v 1 asr rd arithmetic shift right rd(n) rd(n+1), n=0..6 z,c,n,v 1 swap rd swap nibbles rd(3..0) rd(7..4),rd(7..4) rd(3..0) none 1 bset s flag set sreg(s) 1 sreg(s) 1 bclr s flag clear sreg(s) 0 sreg(s) 1 bst rr, b bit store from register to t t rr(b) t 1 bld rd, b bit load from t to register rd(b) tnone1 sec set carry c 1c1 clc clear carry c 0 c 1 sen set negative flag n 1n1 cln clear negative flag n 0 n 1 sez set zero flag z 1z1 clz clear zero flag z 0 z 1 sei global interrupt enable i 1i1 cli global interrupt disable i 0 i 1 ses set signed test flag s 1s1 cls clear signed test flag s 0 s 1 sev set twos complement overflow. v 1v1 clv clear twos complement overflow v 0 v 1 set set t in sreg t 1t1 clt clear t in sreg t 0 t 1 seh set half carry flag in sreg h 1h1 clh clear half carry flag in sreg h 0 h 1 data transfer instructions mov rd, rr move between registers rd rr none 1 movw rd, rr copy register word rd+1:rd rr+1:rr none 1 ldi rd, k load immediate rd knone1 ld rd, x load indirect rd (x) none 2 ld rd, x+ load indirect and post-inc. rd (x), x x + 1 none 2 ld rd, - x load indirect and pre-dec. x x - 1, rd (x) none 2 ld rd, y load indirect rd (y) none 2 ld rd, y+ load indirect and post-inc. rd (y), y y + 1 none 2 ld rd, - y load indirect and pre-dec. y y - 1, rd (y) none 2 ldd rd,y+q load indirect with displacement rd (y + q) none 2 ld rd, z load indirect rd (z) none 2 ld rd, z+ load indirect and post-inc. rd (z), z z+1 none 2 ld rd, -z load indirect and pre-dec. z z - 1, rd (z) none 2 ldd rd, z+q load indirect with displacement rd (z + q) none 2 lds rd, k load direct from sram rd (k) none 2 st x, rr store indirect (x) rr none 2 st x+, rr store indirect and post-inc. (x) rr, x x + 1 none 2 st - x, rr store indirect and pre-dec. x x - 1, (x) rr none 2 st y, rr store indirect (y) rr none 2 st y+, rr store indirect and post-inc. (y) rr, y y + 1 none 2 st - y, rr store indirect and pre-dec. y y - 1, (y) rr none 2 std y+q,rr store indirect with displacement (y + q) rr none 2 st z, rr store indirect (z) rr none 2 st z+, rr store indirect and post-inc. (z) rr, z z + 1 none 2 st -z, rr store indirect and pre-dec. z z - 1, (z) rr none 2 std z+q,rr store indirect with displacement (z + q) rr none 2 sts k, rr store direct to sram (k) rr none 2 lpm load program memory r0 (z) none 3 lpm rd, z load program memory rd (z) none 3 lpm rd, z+ load program memory and post-inc rd (z), z z+1 none 3 spm store program memory (z) r1:r0 none - in rd, p in port rd pnone1 out p, rr out port p rr none 1 push rr push register on stack stack rr none 2 pop rd pop register from stack rd stack none 2 mcu control instructions nop no operation none 1 sleep sleep (see specific descr. for sleep function) none 1 wdr watchdog reset (see specific descr. for wdr/timer) none 1 break break for on-chip debug only none n/a mnemonics operands description operation flags #clocks
256 8246a?avr?11/09 attiny2313a/4313 25. ordering information 25.1 attiny2313a notes: 1. this device can also be supplied in wafer form. please c ontact your local atmel sales office for detailed ordering info rmation and minimum quantities. 2. pb-free packaging, complies to the european directive for restriction of hazardous substances (rohs directive). also halide free and fully green. 3. for speed vs. v cc , see ?speed grades? on page 196 . 4. nipdau finish 5. topside marking for attiny2313a: ? 1st line: t2313 ? 2nd line: axx ? 3rd line: xxx speed (mhz) power supply ordering code (2) package (1) operation range 20 (3) 1.8 - 5.5v attiny2313a-pu attiny2313a-su ATTINY2313A-MU attiny2313a-mmh (4)(5) 20p3 20s 20m1 20m2 industrial (-40 c to 85 c) package type 20p3 20-lead, 0.300" wide, plastic dual inline package (pdip) 20s 20-lead, 0.300" wide, plastic gull wing small outline package (soic) 20m1 20-pad, 4 x 4 x 0.8 mm body, quad flat no-lead / micro lead frame package (mlf) 20m2 20-pad, 3 x 3 x 0.85 mm body, very thin quad flat no lead package (vqfn)
257 8246a?avr?11/09 attiny2313a/4313 25.2 attiny4313 notes: 1. this device can also be supplied in wafer form. please c ontact your local atmel sales office for detailed ordering info rmation and minimum quantities. 2. pb-free packaging, complies to the european directive for restriction of hazardous substances (rohs directive). also halide free and fully green. 3. for speed vs. v cc , see ?speed grades? on page 196 . 4. nipdau finish 5. topside marking for attiny4313: ? 1st line: t4313 ? 2nd line: xx ? 3rd line: xxx speed (mhz) power supply ordering code (2) package (1) operation range 20 (3) 1.8 - 5.5v attiny4313-pu attiny4313-su attiny4313-mu attiny4313-mmh (4)(5) 20p3 20s 20m1 20m2 industrial (-40 c to 85 c) package type 20p3 20-lead, 0.300" wide, plastic dual inline package (pdip) 20s 20-lead, 0.300" wide, plastic gull wing small outline package (soic) 20m1 20-pad, 4 x 4 x 0.8 mm body, quad flat no-lead/micro lead frame package (mlf) 20m2 20-pad, 3 x 3 x 0.85 mm body, very thin quad flat no lead package (vqfn)
258 8246a?avr?11/09 attiny2313a/4313 26. packaging information 26.1 20p3 2325 orchard parkway san jose, ca 95131 title drawing no. r rev. 20p3 , 20-lead (0.300"/7.62 mm w ide) plastic dual inline package (pdip) c 20p3 1/12/04 pin 1 e1 a1 b e b1 c l seating plane a d e eb ec common dimensions (unit of measure = mm) symbol min nom max note a ? ? 5.334 a1 0.3 8 1? ? d 25.493 ? 25.9 8 4 note 2 e 7.620 ? 8 .255 e1 6.096 ? 7.112 note 2 b 0.356 ? 0.559 b1 1.270 ? 1.551 l 2.921 ? 3. 8 10 c 0.203 ? 0.356 eb ? ? 10.922 ec 0.000 ? 1.524 e 2.540 typ notes: 1. this package conforms to jedec reference ms-001, variation ad. 2. dimensions d and e1 do not include mold flash or protrusion. mold flash or protrusion shall not exceed 0.25 mm (0.010").
259 8246a?avr?11/09 attiny2313a/4313 26.2 20s
260 8246a?avr?11/09 attiny2313a/4313 26.3 20m1 2325 orchard parkway san jose, ca 95131 title drawing no. r rev. 20m1 , 20-pad, 4 x 4 x 0.8 mm body, lead pitch 0.50 mm, a 20m1 10/27/04 2.6 mm exposed pad, micro lead frame package (mlf) a 0.70 0.75 0.80 a1 ? 0.01 0.05 a2 0.20 ref b 0.18 0.23 0.30 d 4.00 bsc d2 2.45 2.60 2.75 e 4.00 bsc e2 2.45 2.60 2.75 e 0.50 bsc l 0.35 0.40 0.55 side view pin 1 id pin #1 notch (0.20 r) bottom view top view note: reference jedec standard mo-220, fig . 1 (saw singulation) wggd-5. common dimensions (unit of measure = mm) symbol min nom max note d e e a2 a1 a d2 e2 0.08 c l 1 2 3 b 1 2 3
261 8246a?avr?11/09 attiny2313a/4313 26.4 20m2 title drawing no. gpc rev. package drawing contact: p a ck a gedr a wing s @ a tmel.com 20m2 zfc b 20m2, 20-pad, 3 x 3 x 0.85 mm body, lead pitch 0.45 mm, 1.55 x 1.55 mm exposed pad, thermally enhanced plastic very thin quad flat no lead package (vqfn) 10/24/0 8 15 14 1 3 12 11 1 2 3 4 5 16 17 1 8 19 20 10 9 8 7 6 d2 e2 e b k l pin #1 ch a mfer (c 0. 3 ) d e s ide view a1 y pin 1 id bottom view top view a1 a c c0.1 8 ( 8 x) 0. 3 ref (4x) common dimen s ion s (unit of me asu re = mm) s ymbol min nom max note a 0.75 0. 8 0 0. 8 5 a1 0.00 0.02 0.05 b 0.17 0.22 0.27 c 0.152 d 2.90 3 .00 3 .10 d2 1.40 1.55 1.70 e 2.90 3 .00 3 .10 e2 1.40 1.55 1.70 e ? 0.45 ? l 0. 3 5 0.40 0.45 k 0.20 ? ? y 0.00 ? 0.0 8
262 8246a?avr?11/09 attiny2313a/4313 27. errata the revision letters in this section refer to the revision of the corresponding attiny2313a/4313 device. 27.1 attiny2313a 27.1.1 rev. d no known errata. 27.1.2 rev. a ? c these device revisions were referred to as attiny2313/attiny2313v. 27.2 attiny4313 27.2.1 rev. a no known errata.
263 8246a?avr?11/09 attiny2313a/4313 28. datasheet revision history 28.1 rev. 8246a ? 11/09 1. initial revision. created from document 2543_t2313. 2. updated datasheet template. 3. added vqfn in the pinout figure 1-1 on page 2 . 4. added section 7.2 ?software bod disable? on page 34 . 5. added section 7.3 ?power reduction register? on page 34 . 6. updated table 7-2, ?sleep mode select,? on page 36 . 7. added section 7.5.3 ?bodcr ? brown-out detector control register? on page 37 . 8. added reset disable function in figure 8-1 on page 38 . 9. added pin change interrupts pcint1 and pcint2 in table 9-1 on page 47 . 10. added pcint17..8 and pcmsk2..1 in section 9.2 ?external interrupts? on page 48 . 11. added section 9.3.4 ?pcmsk2 ? pin change mask register 2? on page 52 . 12. added section 9.3.5 ?pcmsk1 ? pin change mask register 1? on page 52 . 13. updated section 10.2.1 ?alternate func tions of port a? on page 61 . 14. updated section 10.2.2 ?alternate func tions of port b? on page 62 . 15. updated section 10.2.3 ?alternate func tions of port d? on page 66 . 16. added umsel1 and umsel0 in section 14.10.4 ?ucsrc ? usart control and sta- tus register c? on page 139 . 17. added section 15. ?usart in spi mode? on page 145 . 18. added usi buffer register (usibr) in section 16.2 ?overview? on page 155 and in fig- ure 16-1 on page 155 . 19. added section 16.5.4 ?usibr ? usi buffer register? on page 166 . 20. updated section 19.6.3 ?reading device signature imprint table from firmware? on page 175 . 21. updated section 19.9.1 ?spmcsr ? store program memory control and status regis- ter? on page 176 . 22. added section 20.3 ?device signature imprint table? on page 180 . 23. updated section 20.3.1 ?calibration byte? on page 181 . 24. changed bs to bs1 in section 20.6.13 ?reading the signature bytes? on page 189 . 25. updated section 21.2 ?dc characteristics? on page 195 . 26. added section 22.1 ?effect of power reduction? on page 203 . 27. updated characteristic plots in section 22. ?typical characteristics? for attiny2313a (pages 204 - 227 ), and added plots for attiny4313 (pages 228 - 251 ). 28. updated section 23. ?register summary? on page 252 . 29. updated section 25. ?ordering information? on page 256 , added the package type 20m2 and the ordering code -mmh (vqfn), and added the topside marking note.
264 8246a?avr?11/09 attiny2313a/4313
i 8246a?avr?11/09 attiny2313a/4313 table of contents features .............. ................ ................ ............... .............. .............. ............ 1 1 pin configurations ... ................ ................ ................. ................ ............... 2 1.1 pin descriptions .................................................................................................2 2 overview ................ .............. .............. ............... .............. .............. ............ 4 2.1 block diagram ...................................................................................................4 2.2 comparison between attiny2313a and at tiny4313 ........................................5 3 about ............. ................ ................. ................ ................. .............. ............ 6 3.1 resources .........................................................................................................6 3.2 code examples .................................................................................................6 3.3 data retention ...................................................................................................6 3.4 disclaimer ..........................................................................................................6 4cpu core ............... .............. .............. ............... .............. .............. ............ 7 4.1 architectural overview .......................................................................................7 4.2 alu ? arithmetic logic unit ...............................................................................8 4.3 status register ..................................................................................................8 4.4 general purpose register file ..........................................................................9 4.5 stack pointer ...................................................................................................11 4.6 instruction execution timing ...........................................................................11 4.7 reset and interrupt handling ...........................................................................12 5 memories ............... .............. .............. ............... .............. .............. .......... 15 5.1 in-system reprogrammabl e flash program memory .....................................15 5.2 sram data memory ........................................................................................16 5.3 eeprom data memory . ................. ................ ............. ............ ............. ..........17 5.4 i/o memory ......................................................................................................21 5.5 register description ........................................................................................21 6 clock system ........... ................ ................ ................. ................ ............. 24 6.1 clock subsystems ...........................................................................................24 6.2 clock sources .................................................................................................25 6.3 system clock prescaler ..................................................................................29 6.4 clock output buffer .........................................................................................30 6.5 register description ........................................................................................30 7 power management and sleep modes ........... .............. .............. .......... 33
ii 8246a?avr?11/09 attiny2313a/4313 7.1 sleep modes ....................................................................................................33 7.2 software bod disable .....................................................................................34 7.3 power reduction register ...............................................................................34 7.4 minimizing power consumption ......................................................................35 7.5 register description ........................................................................................36 8 system control and reset .... .............. .............. .............. .............. ........ 38 8.1 resetting the avr ...........................................................................................38 8.2 reset sources .................................................................................................39 8.3 internal voltage reference ..............................................................................41 8.4 watchdog timer ..............................................................................................41 8.5 register description ........................................................................................44 9 interrupts ............... .............. .............. ............... .............. .............. .......... 47 9.1 interrupt vectors ..............................................................................................47 9.2 external interrupts ...........................................................................................48 9.3 register description ........................................................................................50 10 i/o-ports ............... ................ .............. ............... .............. .............. .......... 54 10.1 ports as general digital i/o .............................................................................55 10.2 alternate port functions ..................................................................................59 10.3 register description ........................................................................................68 11 8-bit timer/counter0 with pw m .............. ................. ................ ............. 70 11.1 features ..........................................................................................................70 11.2 overview ..........................................................................................................70 11.3 clock sources .................................................................................................71 11.4 counter unit ....................................................................................................71 11.5 output compare unit .......................................................................................72 11.6 compare match output unit ............................................................................74 11.7 modes of operation .........................................................................................75 11.8 timer/counter timing diagrams ........... ..........................................................79 11.9 register description ........................................................................................81 12 16-bit timer/counter1 ......... .............. ............... .............. .............. .......... 88 12.1 features ..........................................................................................................88 12.2 overview ..........................................................................................................88 12.3 timer/counter clock sources .........................................................................90 12.4 counter unit ....................................................................................................90
iii 8246a?avr?11/09 attiny2313a/4313 12.5 input capture unit ...........................................................................................91 12.6 output compare units .....................................................................................93 12.7 compare match output unit ............................................................................95 12.8 modes of operation .........................................................................................96 12.9 timer/counter timing diagrams ........... ........................................................104 12.10 accessing 16-bit registers ............................................................................106 12.11 register description ......................................................................................110 13 timer/counter0 and timer/counter1 pr escalers ............ .................. 117 13.1 internal clock source ....................................................................................117 13.2 prescaler reset .............................................................................................117 13.3 external clock source ...................................................................................117 13.4 register description ......................................................................................118 14 usart .................. .............. .............. .............. .............. .............. ........... 119 14.1 features ........................................................................................................119 14.2 overview ........................................................................................................119 14.3 clock generation ...........................................................................................120 14.4 frame formats ..............................................................................................123 14.5 usart initialization .......................................................................................124 14.6 data transmission ? the usart transmitter ..............................................125 14.7 data reception ? the usart receiver .......................................................129 14.8 asynchronous data reception ......................................................................132 14.9 multi-processor communication mode ..........................................................135 14.10 register description ......................................................................................136 14.11 examples of baud rate setting .....................................................................141 15 usart in spi mode .......... .............. .............. .............. .............. ........... 145 15.1 features ........................................................................................................145 15.2 overview ........................................................................................................145 15.3 clock generation ...........................................................................................145 15.4 spi data modes and timing ..........................................................................146 15.5 frame formats ..............................................................................................147 15.6 data transfer .................................................................................................149 15.7 avr usart mspim vs. avr spi ................................................................151 15.8 register description ......................................................................................152 16 usi ? universal seri al interface ............ ................. ................ ............. 155 16.1 features ........................................................................................................155
iv 8246a?avr?11/09 attiny2313a/4313 16.2 overview ........................................................................................................155 16.3 functional descriptions .................................................................................156 16.4 alternative usi usage ...................................................................................162 16.5 register description ......................................................................................162 17 analog comparator .......... .............. .............. .............. .............. ........... 167 17.1 register description ......................................................................................167 18 debugwire on-chip debug s ystem .............. .............. .............. ........ 169 18.1 features ........................................................................................................169 18.2 overview ........................................................................................................169 18.3 physical interface ..........................................................................................169 18.4 software break points ...................................................................................170 18.5 limitations of debugwire .............................................................................170 18.6 register description ......................................................................................171 19 self-programming the flash ................. ................. ................ ............. 172 19.1 performing page erase by spm ....................................................................172 19.2 filling the tempor ary buffer (page loading) .................................................172 19.3 performing a page write .................... ...........................................................173 19.4 addressing the flash during self-pro gramming ...........................................173 19.5 eeprom write prevents writing to spmcsr ............. ............ ............. ........174 19.6 reading lock bits, fuse bits and si gnature data from software .................174 19.7 preventing flash corruption ..........................................................................176 19.8 programming time for flash when using spm ............................................176 19.9 register description ......................................................................................176 20 memory programming ........ .............. ............... .............. .............. ........ 178 20.1 program and data memory lock bits ...........................................................178 20.2 fuse bits ........................................................................................................179 20.3 device signature imprint table .....................................................................180 20.4 page size ......................................................................................................181 20.5 parallel programming pa rameters, pin mapping, and commands ...............182 20.6 parallel programming ....................................................................................183 20.7 serial downloading ............................. ...........................................................190 20.8 serial programming pin mapping ....... ...........................................................191 21 electrical characteristics ... .............. ............... .............. .............. ........ 195 21.1 absolute maximum rating s* .........................................................................195
v 8246a?avr?11/09 attiny2313a/4313 21.2 dc characteristics .........................................................................................195 21.3 speed grades ...............................................................................................196 21.4 clock characteristics .....................................................................................197 21.5 system and reset characteristics ................................................................198 21.6 analog comparator characteristics ...............................................................199 21.7 parallel programming characteristics ...........................................................200 21.8 serial programming characteristics ... ...........................................................202 22 typical characteristics ..... .............. .............. .............. .............. ........... 203 22.1 effect of power reduction .............................................................................203 22.2 attiny2313a ..................................................................................................204 22.3 attiny4313 ....................................................................................................228 23 register summary ............ .............. .............. .............. .............. ........... 252 24 instruction set summary ... .............. ............... .............. .............. ........ 254 25 ordering information .......... .............. ............... .............. .............. ........ 256 25.1 attiny2313a ..................................................................................................256 25.2 attiny4313 ....................................................................................................257 26 packaging information ....... .............. ............... .............. .............. ........ 258 26.1 20p3 ..............................................................................................................258 26.2 20s ................................................................................................................259 26.3 20m1 ..............................................................................................................260 26.4 20m2 ..............................................................................................................261 27 errata ........... ................ ................ ................. ................ .............. ........... 262 27.1 attiny2313a ..................................................................................................262 27.2 attiny4313 ....................................................................................................262 28 datasheet revision history .. ................ ................. ................ ............. 263 28.1 rev. 8246a ? 11/09 .......................................................................................263 table of contents............... ................ ................. .............. .............. ........... i
8246a?avr?11/09 headquarters international atmel corporation 2325 orchard parkway san jose, ca 95131 usa tel: 1(408) 441-0311 fax: 1(408) 487-2600 atmel asia unit 1-5 & 16, 19/f bea tower, millennium city 5 418 kwun tong road kwun tong, kowloon hong kong tel: (852) 2245-6100 fax: (852) 2722-1369 atmel europe le krebs 8, rue jean-pierre timbaud bp 309 78054 saint-quentin-en- yvelines cedex france tel: (33) 1-30-60-70-00 fax: (33) 1-30-60-71-11 atmel japan 9f, tonetsu shinkawa bldg. 1-24-8 shinkawa chuo-ku, tokyo 104-0033 japan tel: (81) 3-3523-3551 fax: (81) 3-3523-7581 product contact web site www.atmel.com technical support avr@atmel.com sales contact www.atmel.com/contacts literature requests www.atmel.com/literature disclaimer: the information in this document is provided in connection wi th atmel products. no license, expr ess or implied, by estoppel or otherwise, to any intellectual property right is granted by this document or in connection with the sale of atmel products. except as set forth in atmel?s terms and condi- tions of sale located on atmel?s web site, atmel assumes no li ability whatsoever and disclaims any express, implied or statutor y warranty relating to its products including , but not limited to, the implied warranty of merchantability, fitness for a particu lar purpose, or non-infringement. in no event shall atmel be liable for any direct, indire ct, consequential, punitive, special or i nciden- tal damages (including, without li mitation, damages for loss of profits, business interruption, or loss of information) arising out of the use or inability to use this document, even if atmel has been advised of the possibility of such damages. atmel makes no representations or warranties with respect to the accuracy or comp leteness of the contents of th is document and reserves the rig ht to make changes to specifications and product descriptions at any time without notice. atmel does not make any commitment to update the information contained her ein. unless specifically provided otherwise, atmel products are not suitable for, and shall not be used in, automotive applications. atmel?s products are not int ended, authorized, or warranted for use as components in applications intended to support or sustain life. ? 2009 atmel corporation. all rights reserved. atmel ? , logo and combinations thereof, avr ? and others are registered trademarks or trade- marks of atmel corporation or its subsidiaries. other terms and product names may be trademarks of others.


▲Up To Search▲   

 
Price & Availability of ATTINY2313A-MU

All Rights Reserved © IC-ON-LINE 2003 - 2022  

[Add Bookmark] [Contact Us] [Link exchange] [Privacy policy]
Mirror Sites :  [www.datasheet.hk]   [www.maxim4u.com]  [www.ic-on-line.cn] [www.ic-on-line.com] [www.ic-on-line.net] [www.alldatasheet.com.cn] [www.gdcy.com]  [www.gdcy.net]


 . . . . .
  We use cookies to deliver the best possible web experience and assist with our advertising efforts. By continuing to use this site, you consent to the use of cookies. For more information on cookies, please take a look at our Privacy Policy. X